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 ...
随机推荐
- POJ 1265 Area
有一种定理,叫毕克定理.... Area Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- SpringMVC 表单标签
引入标签库 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" ...
- 微信公众平台开放JS-SDK(微信内网页开发工具包)
微信公众平台开放JS-SDK(微信内网页开发工具包),这次开放接口是质的飞跃,是对开发者和广大用户一个利好的消息.未来的公众号图文消息会更丰富多彩,准备脑洞大开吧!(第三方平台正式支持接入微信公众平台 ...
- 宿主系统为Ubuntu 14,CentOS 6.5 安装VirtualBox增强工具失败:Building the OpenGL support module[FAILED]
安装先前的笔记:CentOS 6.3 中安装VirtualBOX增强工具失败:Building the main Guest Additions module[FAILED],执行了以下命令 #安装 ...
- post 之checkbox 未被选中解决方法
第一种方法: http://cnn237111.blog.51cto.com/2359144/1293812 第二种方法(推荐): http://blog.csdn.net/xyanghomepage ...
- MySQL Python教程(4)
Class cursor.MySQLCursorBuffered 该类从Class cursor.MySQLCursorBuffered继承,如果需要,可以在执行完SQL语句后自动缓冲结果集合.imp ...
- Android ListView 图片异步加载和图片内存缓存
开发Android应用经常需要处理图片的加载问题.因为图片一般都是存放在服务器端,需要联网去加载,而这又是一个比较耗时的过程,所以Android中都是通过开启一个异步线程去加载.为了增加用户体验,给用 ...
- HDU 5120 Intersection(2014北京赛区现场赛I题 计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5120 解题报告:给你两个完全相同的圆环,要你求这两个圆环相交的部分面积是多少? 题意看了好久没懂.圆环 ...
- 设计模式(15)-Facade Pattern
http://www.cnblogs.com/zhenyulu/articles/55992.html 一. 门面(Facade)模式 外部与一个子系统的通信必须通过一个统一的门面(Facade)对象 ...
- iOS权限管理思路
iOS权限的分类: 1.功能级权限管理 2.数据级权限管理 权限的获取方式: 通常通过网络获取,会有延迟 使用到权限的场景: 1. 点击发送会员专享魔法表情, 判断是否有发送会员专享魔法表情的权限, ...