题目如下:

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. orale--varchar2(5) vs varchar2(5 byte) vs varchar2(5 char)

    varchar2(5) == varchar2(5 byte)------> 'abcde' 但是中文不是5 个字符 varchar2(5 char)----> 'abcde'

  2. python参考手册--第9章

    1.读取命令行选项 (1)sys.args python启动时,命令行选项设置在列表sys.args中. sys.args[0]:xxx.py sys.args[1:]: 其他参数 (2)optpar ...

  3. ORACLE 更新关联多张表

    UPDATE T_XMLC_BILL_HEAD_BAK T1 SET (T1.SENDRECEIVEFLAG, T1.SENDRECEIVEOPERATOR, T1.SENDRECEIVEDATE, ...

  4. linux xxd 命令

    http://www.cnblogs.com/openix/archive/2012/04/23/2466320.html xxd -i dht.jpg dht.h

  5. java:复写equals实例

    class User { String name; int age; /* *比较过程思路: *1.两个对象指向位置相同,那么他们就相等,return后跳出函数,不再往下执行 *2.指向位置不同,有3 ...

  6. 186. Reverse Words in a String II

    题目: Given an input string, reverse the string word by word. A word is defined as a sequence of non-s ...

  7. Armitage主屏幕说明与命令行启动

    (1)我们将Armitage主屏幕标注为A.B和C A:该区域显示预配置的模块.您可以在模块列表下面的文本框中输入要查找的模块进行查找. B:该区域显示我们可以进行漏洞测试的活跃主机. C:该区域显示 ...

  8. &&运算符和||运算符的优先级问题

    package priority; public class TestAndOrPriority { /* * &&的优先级高就不代表 他会先运行 ||的右边 而是说会把右边用& ...

  9. ARM7ldr指令与ldr伪指令

    ldr伪指令的第二个操作数之前有个=,意思是第一个操作书 = 第二个操作数,相当明了 核心就在于对于用.word指令在.text段里另外定义一段内存,用ldr r0,[pc + x(可以算出.text ...

  10. 需要保存数据zabbix,不需要保存数据nagios

    需要保存数据zabbix,不需要保存数据nagios cacti 有什么好用的基于Web的Linux系统监控开源工具(网管系统) 要求类似于Ubuntu的Landscape,可以记录下历史CPU数值. ...