题目如下:

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

 

An example is the root-to-leaf path 1->2->3 which represents the number 123.

 

Find the total sum of all root-to-leaf numbers.

 

For example,

 

    1

   / \

  2   3

The root-to-leaf path 1->2 represents the number 12.

The root-to-leaf path 1->3 represents the number 13.

 

Return the sum = 12 + 13 = 25.

解题思路:

一个带有记录功能的DFS,随时记录当前数字,当前和,返回即可

代码如下:

/**

 * Definition for binary tree

 * struct TreeNode {

 *     int val;

 *     TreeNode *left;

 *     TreeNode *right;

 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}

 * };

 */

class Solution {

public:

    int sumNumbers(TreeNode *root) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        

        int sum = 0;

        int curNumber = 0;

        

        sumNumbers_my(root,curNumber,sum);

        

        return sum;

        

    }

    void sumNumbers_my(TreeNode *root,int &curNumber,int &sum)

    {

        if(root == NULL)return;

        

        curNumber = curNumber*10+root->val;

        int curNumber_bk = curNumber;

        

        if(root ->left== NULL&&root->right ==NULL)

        {

            sum+= curNumber;return;

        }

        

        sumNumbers_my(root->left,curNumber,sum);

        curNumber = curNumber_bk;

        sumNumbers_my(root->right,curNumber,sum);

        return;

    }

};

leetcode—sum root to leaf number的更多相关文章

  1. LeetCode: Sum Root to Leaf Numbers 解题报告

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  2. [leetcode]Sum Root to Leaf Numbers @ Python

    原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...

  3. [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  4. Leetcode Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  5. LeetCode: Sum Root to Leaf Numbers [129]

    [题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a n ...

  6. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

    Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...

  7. LeetCode -- Sum Root to Leaf NNumbers

    Related Links: Path Sum: http://www.cnblogs.com/little-YTMM/p/4529982.html Path Sum II: http://www.c ...

  8. LeetCode :: Sum Root to Leaf Numbers [tree、dfs]

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  9. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

随机推荐

  1. Unity寻路的功能总结

    源地址:http://blog.csdn.net/sgnyyy/article/details/21878163 1. 利用Unity本身自带的NavMesh 这篇文章已经比较详细,可能对于很多需要a ...

  2. Android支付接入(三):电信爱游戏支付

    原地址:http://blog.csdn.net/simdanfeg/article/details/9011977 注意事项: 1.电信要求必须先启动电信的闪屏界面 2.非网络游戏不允许有Inter ...

  3. HDU 1422 重温世界杯(DP)

    点我看题目 题意 : 中文题不详述. 思路 : 根据题目描述及样例可以看出来,如果你第一个城市选的是生活费减花费大于等于0的时候才可以,最好是多余的,这样接下来的就算是花超了(一定限度内的花超),也可 ...

  4. 12 求1+2+...+n

    参考 http://www.cppblog.com/zengwei0771/archive/2012/04/28/173014.html 和 http://blog.csdn.net/shiren_b ...

  5. 阿里巴巴Java面试题

    研二是需要找实习的时候了,因阿里有同学内推就直接参加了电话面试,不说其他的废话直接上问题,阿里的面试官还是不错的,和蔼可亲,为人谦虚,大牛什么都懂.(投的职位是java研发)1.java中所有类的父类 ...

  6. Git教程之分支管理之一

    分支在实际中有什么用呢? 你创建了一个属于你自己的分支,别人看不到,别人还继续在原来的分支上正常工作,而你在自己的分支上干活,想提交就提交,直到开发完毕后,再一次性合并到原来的分支上,这样,既安全,又 ...

  7. 哈希值识别工具hash-identifier

    Hash Identifier可以用来识别各种类型的哈希值.在kali上使用方法很简单 (1)搜索hash-identifier (2)在HASH后面输入要识别的hash内容 (3)识别成功 wind ...

  8. 基于Android Studio搭建Android应用开发环境

    备注:电脑是windows xp系统 1.     安装JDK和环境变量设置 JDK是java development kit,Java JDK下载地址 http://www.oracle.com/t ...

  9. mvn命令

    打包:mvn package 编译:mvn compile 编译测试程序:mvn test-compile 清空:mvn clean 运行测试:mvn test 生成站点目录: mvn site 生成 ...

  10. UVa 10213 (欧拉公式+Java大数) How Many Pieces of Land ?

    题意: 一块圆形土地,在圆周上选n个点,然后两两连线,问把这块土地分成多少块? 分析: 首先紫书上的公式是错的,不过根据书上提供的思路很容易稍加修改得到正确答案! 然后推公式吧,这里用到平面图的欧拉公 ...