[leetcode]_Sum Root to Leaf Numbers
题目:计算一棵二叉树所有路径组成的数的总和。

思考:也是DFS的基础应用。虽然还是套着别人的DFS框架写的,但是学习通常会经历先模拟,再创新的过程。
代码:
private int sum = 0;
public int sumNumbers(TreeNode root) {
dfs(root , 0);
return sum;
}
public void dfs(TreeNode node , int tempSum){
if(node == null) return ; tempSum = tempSum * 10 + node.val;
if(node.left == null && node.right == null) {
sum += tempSum;
return;
} dfs(node.left , tempSum);
dfs(node.right , tempSum);
}
[leetcode]_Sum Root to Leaf Numbers的更多相关文章
- 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 OJ--Sum Root to Leaf Numbers
https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 给一棵树,找从根到叶的路径,并把路径上的数相加,求和. 树的深度优先搜索 /** ...
- [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 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 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 ...
随机推荐
- 手机端的各种默认样式比如 ios的按钮变灰色
1.ios按钮变灰色,给按钮加样式: -webkit-appearance: none; 2.有圆角话 ; } 3.去除Chrome等浏览器文本框默认发光边框 input:focus, textare ...
- java GUI之基本图形
1.为了支持图形用户界面程序设计,java1.0的标准类库中包含一个抽象窗口工具箱(Abstract Window Toolkit,AWT). 这个工具箱极不成熟,其编程模型也不是面向对象的,有很大的 ...
- vim 配合管道过滤多行记录
vim打开一个日志 有很多冗余信息,你只想看到一部分的内容,怎么办? 在normal模式输入 :%!grep xxx 这样,所有含有xxx的行才会被保留下来,其它行都不见了.. 或者,你想干掉所有包含 ...
- java的新窗体
1.JFrame窗体 jf.setSize(200, 150); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); ...
- (转)C#使用Mysql记录
(1)首先需要下载C#访问MySQL数据库的ADO.NET驱动程序 http://dev.mysql.com/downloads/connector/ 我下载的版本为: mysql-connector ...
- 在git上下载的Asp.Net MVC 4源码怎么编译?
以本人的下载位置为例:E:\aspnetwebstack 1.win+r 输入cmd 打开dos 界面 2.e: 回车,定位到e 盘 3.cd E:\aspnetwebstack 进入e 盘aspne ...
- ibatis配置多表关联(一对一、一对多、多对多)
iBatis的多表关联. ibatis的表关联,和数据库语句无关,是在程序中,把若干语句的结果关联到一起.这种关联形式,虽然在大数据量时是很奢侈的行为,但是看起来很干净,用起来也很方便. 这里用表lo ...
- I/B/P SP/SI
H264中I.B.P帧 I帧:只作为参考帧,采用帧内预测 B帧:以其前面的I帧或P帧和后面的P帧作为参考帧 P帧:只能以前面的I帧或P帧作为参考帧 H264中的SP SI SP和SI是H264中引入的 ...
- Effective Modern C++翻译(7)-条款6:当auto推导出意外的类型时,使用显式的类型初始化语义
条款6:当auto推导出意外的类型时,使用显式的类型初始化语义 条款5解释了使用auto来声明变量比使用精确的类型声明多了了很多的技术优势,但有的时候,当你想要zag的时候,auto可能会推导出了zi ...
- -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
在有全屏侧滑的情况下,页面上有个slider需要左右滑动的时候,经常在滑动slider的时候页面也跟着滑动 解决办法一:关闭当前页面的全屏侧滑,开启系统侧滑 self ...