129. Sum Root to Leaf Numbers
题目:
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.
代码:
作为第一个做的Medium级别的题目,还是有点困难的。
关键两步都参考了百度:1. 递归的方式创建一颗二叉树(用来测试);2. 递归的方式深度优先遍历二叉树并求和。
1. 递归的方式创建一颗二叉树(用来测试):
递归方法简历二叉树,就不多说了,有兴趣去调试一下,一目了然。
public TreeNode(int[] array){
root=makeBinaryTreeByArray(array,1);
}
/**
* 采用递归的方式创建一颗二叉树
* 传入的是二叉树的数组表示法
* 构造后是二叉树的二叉链表表示法
*/
public static TreeNode makeBinaryTreeByArray(int[] array,int index){
// System.out.print("递归建树数组剩余: "+index+array.length);
if(index<array.length){
int value=array[index];
// if(array[index]!=0){
TreeNode t=new TreeNode(value);
array[index]=0;
t.left=makeBinaryTreeByArray(array,index*2);
t.right=makeBinaryTreeByArray(array,index*2+1);
return t;
// }
}
return null;
}
2. 递归的方式深度优先遍历二叉树并求和:
从root节点开始,调用sumNumbers(),首先将root的值计入变量tmpsum。
如果左子树不为空,以左子树节点为参数,递归调用sumNumbers()函数,此时第一次记录root的值*10加上该节点的值。
左子树优先处理,所以会一直调用完,相当于深度优先遍历,组合出全部左树的结果。
左子树处理之后就回去遍历右子树,每次都是先左后右。
如果右子树不为空,同样以右子树节点为参数,递归调用sumNumbers()函数,此时第一次记录root的值*10加上该节点的值。
当某个节点左右子树都未空时,说明当前已经是最下层节点了,于是*10+val生成本条路径的tmpsum,之后加入变量result中。
解释了这么多,肯定还是懵逼,自己去画一个二叉树,试一试就发现,是这个样子滴!
int result;
public int sumNumbers(TreeNode root) {
if(root==null){
System.out.println("empty tree");
return 0;
}
result =0 ;
sumnuber(root,0);
System.out.print(result);
return result;
}
public int sumnuber(TreeNode root,int tmpsum) {
if(root.left == null && root.right == null)
{
result += tmpsum * 10 + root.val;
// System.out.print(result+" ");
}
if(root.left != null)
{
sumnuber(root.left, tmpsum * 10 + root.val);
}
if(root.right != null)
{
sumnuber(root.right, tmpsum * 10 + root.val);
}
return result;
}
129. Sum Root to Leaf Numbers的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)
Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...
- LeetCode OJ 129. Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- 129. Sum Root to Leaf Numbers pathsum路径求和
[抄题]: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a ...
- [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [LC] 129. 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 129. Sum Root to Leaf Numbers ----- java
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 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] 129. Sum Root to Leaf Numbers 解题思路
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- 【学习】一本案例驱动的jQuery Mobile入门书
清华大学出版社推出的<构建跨平台APP:jQuery Mobile移动应用实战> 提供的全是jQuery Mobile的案例: 手机入侵 题库应用 音乐专辑 通讯录 新闻列表 销售排名 九 ...
- isNaN() 确认是否是数字
isNaN(x): 当变量 x 不是数字,返回 true: 当变量 x 是其他值,(比如,1,2,3),返回false.
- bootstrap搜索框样式代码及效果
<div class="container"> <div class="input-group"> <input type=&qu ...
- rpc优化
1.刷文章列表的时候,发现调用总时间100ms ,其中调策略是花了60ms,一个开源的map方法dozer,组装bean要花40ms 2.redis的zounct方法,传 1和-1的时候有时候会返回0 ...
- mysql 设置可以外部访问
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; --授权可以外部 ...
- Unity手游之路<八>自动寻路Navmesh之入门
http://blog.csdn.net/janeky/article/details/17457533 在的大部分mmo游戏都有了自动寻路功能.点击场景上的一个位置,角色就会自动寻路过去.中间可能会 ...
- C#构造方法重载
1.什么是构造方法? 首先,它是一个方法,它是类中 众多方法中的一个.其次,它具有类中其他方法所不具备的一些特性. 在类执行开始的时候,执行这个方法. 2.构造方法相对其他方法有哪些不同? 方法名:类 ...
- Dom终
l创建DOM元素 •createElement(标签名) 创建一个节点 •appendChild(节点) 追加一个节点 –例子:为ul插入li <!DOCTYPE html PUBLIC & ...
- discuz论坛移植修改数据库配置
从其他地方拷贝的discuz源码,可能需要修改数据库配置 分别打开discuz目录下面以下三个文件 discuzRoot/uc_server/data/config.inc.phpdiscuzRoot ...
- 怎么安装MYSQL5.0的JDBC驱动
1.下载mysql for jdbc driver. http://dev.mysql.com/downloads/connector/j/3.1.htmlMySQL Connector/J is ...