[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 ...
随机推荐
- cvs 用法
CVS使用指南 1 概念 CVS是Client/Server结构的并行版本控制系统. 资源库(repository) 存在于服务器上,所有版本的数据仓库.可以把它想象成一个数据库服务器. 模块 (mo ...
- 使用tcpdump+Wireshark(或Fiddler)做linux服务器的网络请求分析
我们的服务器上,一般都没有窗口界面,这时候要抓包,用tcpdump是最方便的.而分析网络请求时,wireshark又是相当方便的,这时候我们就需要把它们两个一起来使用了. tcpdump 抓取数据 命 ...
- SQL行转列汇总
PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P ...
- Android JNI学习之javah命令的正确使用(找了好半天才找到的,汉,网上好多说法都没用)
按照网上抄来的javah用法一般出错,今天查了一下午在一篇文章(http://www.ibm.com/developerworks/cn/java/j-jtctips/part6/index2.htm ...
- 【翻译习作】 Windows Workflow Foundation程序开发-前言
Windows Workflow Foundation程序开发-基于XAML和C#的WF实战技术与例程 ——C#程序员的WF功能与编程接口技术指导 前言 Windows Workflow Founda ...
- UIApplication介绍
一.什么是UIApplication UIApplication对象是应用程序的象征. 每一个应用都有自己的UIApplication对象,这个对象是系统自动帮我们创建的, 它是一个单例对象. 一个i ...
- linux C中va_list用法
#include <stdio.h> #include <stdarg.h> int demo( int, ... ); int main( void ) { demo(1, ...
- 华为OJ平台——密码强度等级
题目描述: 密码按如下规则进行计分,并根据不同的得分为密码进行安全等级划分. 一.密码长度: 5 分: 小于等于4 个字符 10 分: 5 到7 字符 25 分: 大于等于8 个字符 二.字母: 0 ...
- 华为OJ平台——求最大连续bit数
题目描述: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1 输入: 一个byte型的数字 输出: 对应的二进制数字中1的最大连续数 思路: ...
- JQ仿select框
点击[cy_title]后弹出[cy_list]层,选中里面的元素把值赋给 [cy_title] 在[cy_list] 打开的时候,点击其他地方可以关闭: HTML: <div class=&q ...