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 ...
随机推荐
- 错误:[将截断字符串或二进制数据。\r\n语句已终止。]
错误:[将截断字符串或二进制数据.\r\n语句已终止.] 解决方法是将数据库表这列的长度调大一点
- PAT-乙级-1014. 福尔摩斯的约会 (20)
1014. 福尔摩斯的约会 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 大侦探福尔摩斯接到一张奇怪的 ...
- windows下游戏服务器端框架Firefly安装说明及demo运行
原地址:http://blog.csdn.net/wangqiuyun/article/details/11150503 本来公司一个网游服务器端选定了pomelo框架,后来出了个Firefly,为做 ...
- Chp2: Linked List
2.2 Implement an algorithm to find the kth to last element of a singly linked list. Just using " ...
- MVC项目总结(别人的好文章)
引用 http://www.cnblogs.com/xling/archive/2012/07/11/2587002.html
- C++中 模板Template的使用
1.在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢?答:class用于定义类,在模板引入c++后,最初定义模板的方法 ...
- Java web 项目搭建
Java web 项目搭建 简介 在上一节java web环境搭建中,我们配置了开发java web项目最基本的环境,现在我们将采用Spring MVC+Spring+Hibernate的架构搭建一个 ...
- python 中@property的使用
从14年下半年开始接触到python,自学了一段时间,后又跟别人学习了下,把基础知识基本上学过了.忽然感觉python不可能这么简单吧,就这么点东西?后来看了下书,发现还有很多的高级部分.连续看了两天 ...
- Flash挡住DIV的解决方法
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...
- 【Pure】
PureA set of small, responsive CSS modules that you can use in every web project.http://purecss.io/