[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.
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.
这题是简单地深度搜索bfs,代码如下:
#include <iostream>
using namespace std; /**
* 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) {
unsigned int cur_val=,ret_val=;
help_f(root,cur_val,ret_val);
return ret_val;
}
void help_f(TreeNode *node,unsigned int &cur_val,unsigned int &ret_val)
{
if(node==NULL) return ;
cur_val=cur_val*+node->val;
help_f(node->left,cur_val,ret_val);
help_f(node->right,cur_val,ret_val);
if(node->left==NULL&&node->right==NULL) ret_val+=cur_val;
cur_val/=;
}
}; int main()
{ return ;
}
[LeetCode] Sum Root to Leaf Numbers 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@ [129] Sum Root to Leaf Numbers (DFS)
https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...
- 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)
题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...
- [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 @ 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. ...
随机推荐
- vim正则表达式的替换变量
在正规表达式中使用 \( 和 \) 符号括起正规表达式,即可在后面使用\1.\2 等变量来访问 \( 和 \) 中的内容. 例如有下列英汉对照文本: adapter 适配器address 地址alge ...
- 关于mybatis callSettersOnNulls 配置
今天做了一件坑了自己的事情,为此浪费了好多时间... 在mybatis的设置中,看到了这样的一行设置.出于程序员的好奇,去搜索了一下,这条设置是干什么的. <setting name=" ...
- 【PHP】什么时候使用Try Catch(转)
几条建议: 如果无法处理某个异常,那就不要捕获它. 如果捕获了一个异常,请不要胡乱处理它. 尽量在靠近异常被抛出的地方捕获异常. 在捕获异常的地方将它记录到日志中,除非您打算将它重新抛出. 按 ...
- exec , 元类,__new__, __call__ , 单例模式 , 异常
1,类也是对象 ''' 动态语言 可以在运行期间 动态生成类 修改对象属性 静态语言 ''''' ''' type(object_or_name, bases, dict) type(object) ...
- python 2.7版本解决TypeError: 'encoding' is an invalid keyword argument for this function
今天在用yaml处理数据时,由于yaml.load可接收一个byte字符串,unicode字符串,打开的二进制文件或文本文件对象,但字节字符串和文件必须是utf-8,utf-16-be或utf-16- ...
- 使用JFreeChart生成报表
1.JFreeChart简介 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications,servlets以及JSP等使用所设计. ...
- 【luminate primordial】苏州之行
测试了reader 07版 更主要的是第一次坐了高铁 还不错 路上看到下雨的时候都是水顺着玻璃平着流 好厉害的样子 6个人去的6个人回 今儿开会 老板不太满意 小随意 对我来说,收获感觉还是不小的,使 ...
- [BZOJ3524]区间问题(主席树)
Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. ...
- windows下的命令
1.cd 现在默认只能在当前盘符中改变目录,如果要改变盘符则需要多加一个/d命令. cd /d d:/git/springTest 2.chdir 显示当前目录名或改变当前目录. CHDIR [/D] ...
- HDU 1847 Good Luck in CET-4 Everybody!(SG函数)
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...