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 ...
随机推荐
- Ubuntu14.04 在右键中添加 在终端中打开
1.在terminal中执行: sudo apt-get install nautilus-open-terminal 此时可能会提示:nable to locate package nautilus ...
- CF978A Remove Duplicates【数组操作/STL】
[链接]:CF978A [分析]:逆向思考+标记数组去重 [代码]: #include<bits/stdc++.h> using namespace std; const int INF ...
- Number of Connected Components in an Undirected Graph -- LeetCode
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- Paint Fence -- LeetCode
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- Ubuntu 16.04没有/etc/default/rcS文件的UTC设置选项的问题解决
继续上一篇文章介绍了CentOS的时钟设置:http://www.cnblogs.com/EasonJim/p/8111747.html,大致的知道了BIOS在Linux的时区设置. 而现在关心的问题 ...
- 合理配置SQL Server的最大内存
http://blog.itpub.net/26435490/viewspace-1481846/
- Winform打砖块游戏制作step by step第三节---移动挡板
一 引子 为了让更多的编程初学者,轻松愉快地掌握面向对象的思考方法,对象继承和多态的妙用,故推出此系列随笔,还望大家多多支持. 预备知识,无GDI画图基础的童鞋请先阅读一篇文章让你彻底弄懂WinFor ...
- tiny4412 串口驱动分析三 --- log打印的几个阶段之内核自解压
作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...
- UBI介绍
转:http://blog.csdn.net/kickxxx/article/details/6707589 目录 Table of contents Big red note User-space ...
- debian下QT4编程环境的建立
转:http://moosewoler.blog.163.com/blog/static/6986605200801013442336/ QT是一款跨平台的C++编程framework.QT的主要特性 ...