leetcode—sum root to leaf number
题目如下:
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 3The 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() functionint 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的更多相关文章
- 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 ...
- [leetcode]Sum Root to Leaf Numbers @ Python
原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...
- [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 ...
- 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 ...
- 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 ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- android应用崩溃的调试方法(c++ lib so文件库崩溃)
android调试工具addr2line使用: 1.将ndk中的arm-linux-androideabi-addr2line可执行文件的路径加入配置文件~/.bashrc中,例如: export P ...
- 每个QWidget都有contentsMargins函数,善用QMargins
m_pSearchLineEdit = new QLineEdit(); QPushButton *pSearchButton = new QPushButton(this); pSearchButt ...
- java:I/O 往原文件追加内容
原来txt内容: "我要添加内容" import java.io.*; class Test { public static void main(String[] args) { ...
- 第十七章 委托 第十八章 Attribute 第十九章 可空值类型
1.委托揭秘 定义一个委托,编译器会生成一个继承自System.MulticastDelegate的类,所有的委托都继承自该类. 由于委托是类,所以能定义类的地方,都能定义委托. 委托内部有一个tar ...
- 【剑指offer】求逆序对的个数
2013-09-07 10:50:31 面试题36:在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字构成一个逆序对.输入一个数组,求出这个数组中逆序对的总数. 小结: 最直观的的方法是: ...
- 【HDOJ】4351 Digital root
digital root = n==0 ? 0 : n%9==0 ? 9:n%9;可以简单证明一下n = a0*n^0 + a1*n^1 + ... + ak * n^kn%9 = a0+a1+..+ ...
- linux bash脚本把A和B文件中有相同ID的B文件的内容输出到文件C
bash脚本把A和B文件中有相同ID的B文件的内容输出到文件C. Aid文件:ID001.1ID032.1ID090.10 Bfilt文件:XX XX XXX ID001.1 XXX999999999 ...
- 3月下旬剩余poj题解
poj1700 数学推导+简单dp poj2390 水题不说什么了 poj3260 先对找的钱做完全背包,在对能付的钱做多重背包,注意这道题能付的钱数的上界 poj2516 裸的最小费用最大流了没什么 ...
- Repeater实例应用
在实际开发过程中,涉及到数据绑定,分页,以及一对多展示数据时,遇到这样的需求我们怎么解决呢?下面以帖子展示来逐一说明. 帖子主要由两部分组成,第一部分是发帖人的原创内容部分,第二部分是用户评论部分,这 ...
- UVa 1599 (字典序最小的最短路) Ideal Path
题意: 给出一个图(图中可能含平行边,可能含环),每条边有一个颜色标号.在从节点1到节点n的最短路的前提下,找到一条字典序最小的路径. 分析: 首先从节点n到节点1倒着BFS一次,算出每个节点到节点n ...