题目如下:

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. maven添加jar包依赖

    maven的东西使用了一段时间,但是每次使用都多少有点含糊,所以总结一下. 目前主要使用是在jar包的控制上 原理: 在本地,指定一个文件夹,便是maven的仓库,maven会从远程的中央仓库中下载你 ...

  2. 建立Clojure开发环境-使用IDEA和Leiningen

    OS: Mac OS X 10.10 IDEA 14.0.2 Community Edition 安装Leiningen 按照http://leiningen.org/的指南安装lein 阅读Lein ...

  3. zoj 3640 Help Me Escape 概率DP

    记忆化搜索+概率DP 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...

  4. ArcGIS学习记录—union、merge及append的区别

    原文地址: ArcGIS问题:union.merge及append的主要区别[转] - Silent Dawn的日志 - 网易博客 http://gisman.blog.163.com/blog/st ...

  5. 201. Bitwise AND of Numbers Range

    题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...

  6. dp和px,那些不得不吐槽的故事——Android平台图

    http://blog.sina.com.cn/s/blog_6499f8f101014ipq.html 一个优秀的手机软件,不仅要有精巧的功能,流畅的速度,让人赏心悦目的UI也往往是用户选择的重要理 ...

  7. HTML5学习(七)----地理定位

    参考教程:http://www.w3school.com.cn/html5/html_5_geolocation.asp 说明:设备必须有GPS定位功能才能定位的 定位用户的位置 HTML5 Geol ...

  8. pyqt实践——从裸机到打包安装

    1 安装python 安装python-2.7.6.msi默认在c盘 设置环境变量,path后追加c:/python27.可以在命令行直接认识命令python 2 安装pyqt PyQt4-4.10- ...

  9. 彻底搞清js中闭包(Closure)的概念

    js中闭包这个概念对于初学js的同学来说, 会比较陌生, 有些难以理解, 理解起来非常模糊. 今天就和大家一起来探讨一下这个玩意. 相信大家在看完后, 心中的迷惑会迎然而解. 闭包概念: 闭包就是有权 ...

  10. UVa 1643 Angle and Squares

    题意: 如图,有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 分析: 直观上来看,当这n个正方形的对角线在一条直线上时,封闭区域的面积最大.( ...