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 ...
随机推荐
- @Html.Raw()用法
@Html.Raw() 方法输出带有html标签的字符串, 如:@Html.Raw("<div style='color:red'>输出字符串</div>" ...
- 字符串进行富文本操作后末尾emoji表情显示会乱码
问题描述: 1.UIlabel展示的text中包含emoji表情 2.emoji表情作为最后一个字符 在满足以上条件的前提下末尾的emoji会乱码. 寻找问题过程: 因为是在单独的机型上出现的问题,所 ...
- Hibernate 快速上手操作入门
本文主要是从技术操作角度去介绍hibernate,并且简单描述如何快速的上手操作hibernate,用于作为与数据库的连接的持久层. 简单讲述一下hibernate究竟是什么,它有什么作用,在理解它的 ...
- HDU 1394.Minimum Inversion Number-最小逆序数-完全版线段树(单点增减、区间求和)
HDU1394.Minimum Inversion Number 这个题求最小逆序数,先建一个空的树,然后每输入一个值,就先查询一下,查询之后,更新线段树,然后遍历一遍,每次将第一个数放到最后之后,减 ...
- 线段树【p1607】[USACO09FEB]庙会班车Fair Shuttle
Description 逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼--如果要逛完一整天的集市,他们一定会筋疲力尽的.所以为了让奶牛们也能愉快地逛集市,约翰准备让奶牛 ...
- Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]
hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...
- 十. 图形界面(GUI)设计3.标签、按钮和按钮事件
标签和按钮也许是图形界面中最常见的两种组件,按钮又总是与激发动作事件有关. 标签 标签(JLabel)是最简单的Swing组件.标签对象的作用是对位于其后的界面组件作说明.可以设置标签的属性,即前景色 ...
- Java加密技术(一)—— HMACSHA1 加密算法
HMACSHA1 是从 SHA1 哈希函数构造的一种键控哈希算法,被用作 HMAC(基于哈希的消息验证代码). 此 HMAC 进程将密钥与消息数据混合,使用哈希函数对混合结果进行哈希计算,将所得哈希值 ...
- 在C#中用RX库和await来实现直观的状态机
在程序的设计过程中,我们经常会遇到一些需要使用状态机的场景,相信状态机的编写和维护是令每一个程序员都非常头大的事情.到了C# 5.0后,由于引进了await语法糖,我们可以通过await和Reacti ...
- python里的“__all__ ”作用
转载:http://python-china.org/t/725 参考:http://www.cnblogs.com/alamZ/p/6943869.html 用 __all__ 暴露接口,这是一种约 ...