[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 ...
随机推荐
- Microsoft visual studio中文字样输出
解决办法: 可以尝试下通过: 1.file->高级保存选项-> 2.工具->选项->文本编辑器->自动检测不带签名的UTF-8编码
- log4j 缓存
log4j.appender.A2.bufferedIO=truelog4j.appender.A2.bufferSize=512000
- [技巧]如何清除VS2008的最近打开项目
)删除最近打开的文件 运行regedit,打开HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\FileMRUList 之后,在右边删除相应键 ...
- [SQL]详解CUBE和ROLLUP区别<使用rollup或cube通过交叉列可产生高级汇总结果集>
要使用CUBE,首先要了解GROUP BY. 其实CUBE和ROLLUP区别不太大,只是在基于GROUP BY 子句创建和汇总分组的可能的组合上有一定差别,CUBE将返回的更多的可能组合.如果在GRO ...
- 蓝桥杯---数独(模拟 || dfs)
[编程题](满分33分) "数独"是当下炙手可热的智力游戏.一般认为它的起源是"拉丁方块",是大数 学家欧拉于1783年发明的. 如图[1.jpg]所示:6x6 ...
- xcode C++一些简单设置
下面是一个要用到mysql库的C++程序设置: 添加用户头文件: 双击项目-Build Settings-Search Paths: Library Search Paths: /usr/local/ ...
- 【原】rsync的详细参数
rsync参数的具体解释如下: -v, --verbose 详细模式输出-q, --quiet 精简输出模式-c, --checksum 打开校验开关,强制对文件传输进行校验-a, --archive ...
- Configuring HugePages for Oracle on Linux (x86-64)
Introduction Configuring HugePages Force Oracle to use HugePages (USE_LARGE_PAGES) Disabling Transpa ...
- namespace用法
1.在WCF.Controller中定义了一个UserModel,标记为① 2.在WCF.Controller.Model中定义了一个UserModel(同上,namespace不同),标记为② 3. ...
- 002 The Variables In Csharp
在介绍本章之前,我们先看一下C#的编译过程,如下图所示: 图片摘自:http://www.developingthefuture.net/compilation-process-and-jit-com ...