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.
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的思路,事实上也就是postorder(先序遍历),遍历路径上的值每次乘以基数10,过程非常easy,代码例如以下:
- class Solution {
- public:
- int sum;
- void dfs(TreeNode *root, int pre){
- if (root == NULL)
- return;
- int current = root->val + 10 * pre;
- if (root->left == NULL && root->right == NULL){
- sum += current;
- return;
- }
- if (root->left)
- dfs(root->left, current);
- if (root->right)
- dfs(root->right, current);
- }
- int sumNumbers(TreeNode *root) {
- sum = 0;
- dfs(root, 0);
- return sum;
- }
- };
LeetCode :: Sum Root to Leaf Numbers [tree、dfs]的更多相关文章
- 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 求根到叶节点数字之和
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 @ 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 from0-9only, 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 ...
- 129. 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 ...
- LeetCode Sum Root to Leaf Numbers(DFS)
题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...
随机推荐
- js-offsetX、pageX、clientX、layerX、screenX
真心地我也是懵逼的 clientX,clientY:针对屏幕有效区域,不包括滚动部分,坐标(0,0)一直在有效区域的左上角 X,Y: 针对屏幕有效区域,不包括滚动部分,坐标(0, ...
- HDU 4920.Matrix multiplication-矩阵乘法
Matrix multiplication Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- (10)java基础知识-字符串
String s和 new String 的区别 String s1="hello"; String s2= new String("hello"); Stri ...
- ORA-17003. Invalid column index
sql里面有? 希望输入有参数 java程序里面没有给入参数
- POJ 3420 Quad Tiling (矩阵乘法)
[题目链接] http://poj.org/problem?id=3420 [题目大意] 给出一个4*n的矩阵,求用1*2的骨牌填满有多少方案数 [题解] 弄出不同情况的继承关系,用矩阵递推即可. [ ...
- A folder failed to be renamed or moved--安装Android SDK的问题
对于Android是一直想学却一直未学,行动跟不上想法.现在,终于付诸于行动了. 首先,我找的第一个Android的资料是大话企业级Android,前阵子刚看完大话设计模式,通俗易懂,还是比较喜欢这一 ...
- C# 窗体位置 Show和ShowDialog (转载)
CenterParent 窗体在其父窗体中居中. CenterScreen 窗体在当前显示窗口中居中,其尺寸在 ...
- mysql关于访问权限以及root密码修改
root密码修改:mysql> use mysql;mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = ' ...
- python实现scrapy爬取图片到本地时的sha1摘要算法文件名
2017-03-29 Scrapy爬图片到本地应该会给图片自动生成sha1摘要算法文件名,我第一次用scrapy也不清楚太多,就在程序里自己写了一段实现这一功能的代码.需import hashlib ...
- ECSHOP筛选属性修改title标题
发现百度蜘蛛爬行网站时会爬行属性链接,而且会进行收录.可是ecshop系统制作的网站,在分类页点击属性筛选出产品时,网页title不会改变.这样就会造成大量title一样的页面,不利于优化.为此,在网 ...