题目如下:

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. case class inheritance

    Scala 禁止case class inheritance case class Person(name: String, age: Int) case class FootballPlayer(n ...

  2. 1027-Quicksum

    描述 A checksum is an algorithm that scans a packet of data and returns a single number. The idea is t ...

  3. tomcat安全设置

    1.关闭服务器端口:server.xml默认有下面一行: <Server port="8005" shutdown="SHUTDOWN"> 这样允许 ...

  4. LoadImage 和 BitBlt

    #include <windows.h> #define WINDOWCLASS TEXT("Test") #define WNDTITLE TEXT("Te ...

  5. uboot源码整体框架

    源码解压以后,我们可以看到以下的文件和文件夹:  cpu 与处理器相关的文件.每个子目录中都包括cpu.c和interrupt.c.start.S.u-boot.lds. cpu.c:初始化CPU.设 ...

  6. Linux下安装Wine运行windows程序

    资料 首页 https://www.winehq.org/ 安装 https://www.winehq.org/download/ 教程 https://www.winehq.org/document ...

  7. windows下的go语言的环境搭建和初探

    闲话不说,直入主题. 1.准备工具 a.windows下的Go语言开发安装包 官方下载地址:https://code.google.com/p/go/downloads/list b.Go语言中文官网 ...

  8. Python中的函数对象与闭包

    函数在Python中是第一类对象,可以当做参数传递给其他函数,放在数据结构中,以及作为函数的返回结果. 下面的例子为接受另外一个函数作为输入并调用它 #foo.py def callf(func): ...

  9. memcpy与memmove区别

    头文件:#include <string.h> memmove() 用来复制内存内容,其原型为:    void * memmove(void *dest, const void *src ...

  10. Access增删改查 (持续更新中)

    关于Access数据库(2003)的增删改查,其实和Sql大体差不多,但是还有很多不一样的地方.下面列几个容易犯的错误:  1.Access数据库的位置: conn = new OleDbConnec ...