[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 ...
随机推荐
- 20145305 《Java程序设计》第9周学习总结
教材学习内容总结 1.厂商在操作JDBC驱动程序时,依方式可将驱动程序分为4种类型: JDBC-ODBC Bridge Driver Native API Driver JDBC-Net Driver ...
- [Flex] PopUpButton系列 —— 弹出菜单的行高设置
<?xml version="1.0" encoding="utf-8"?> <!--Flex中如何通过variableRowHeight样式 ...
- javascript自执行匿名函数
1)自执行匿名函数: 常见格式:(function() { /* code */ })(); 2)作用: function中的code代码在解释时就已经在运行了.因此可以用它创建命名空间, ...
- sql server Geometry 类型操作 笔记
sqlGeometry 类型为sql server 2008之后的版本 新加的一种CLR扩展数据类型,为广大sql server开发人员存储几何类型及空间运算提供极大的便利,下面说明geometry类 ...
- Eclispe 安装PropertiesEditor插件
一个不错的编写properties文件的Eclipse插件(plugin),有了它我们在编辑一些简体中文.繁体中文等Unicode文本时,就不必再使用native2ascii编码了.您可以通过Ecli ...
- [HDU 4082] Hou Yi's secret (简单计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4082 题目大意: 给你n个点,问能最多构成多少个相似三角形. 用余弦定理,计算三个角度,然后暴力数有多 ...
- java String 的方法 (01)
1.int compareTo(String other) 按照字典的顺序,如果字符串位于String other 之前,则返回一个负数,如果字符串位于othere之后,则返回一个正数,如果两个字符串 ...
- nginx/Windows-1.9.0的日志分割
@echo offrem @echo off rem 取1天之前的日期echo wscript.echo dateadd("d",-1,date) >%tmp%\tmp.vb ...
- HDU 4407 Sum 容斥原理
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...
- VC++2005下的ADO SQL语句(like,count,distinct)和操作(转)
http://blog.sina.com.cn/s/blog_56fd66a70100hxjf.html http://timke.blog.163.com/blog/#m=0 环境:MFC Dia ...