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 ...
随机推荐
- js(jquery)代码在页面上实时地显示时间
一.引入jquery 二.HTML代码 三.js代码 1)引入js代码 2)下面是完整的js代码
- 大数据之nutch
一.nutch简介 nutch是大名鼎鼎的Doug Cutting发起的爬虫项目,nutch孵化了现在大数据处理框架Hadoop.在nutch V 0.8.0 版本之前,Hadoop是nutch的一部 ...
- MAS 移动业务整合系统
AppCan MAS是基于高性能NODEJS架构开发的企业移动后端整合系统,内置各种标准协议组件,统一移动业务前后端标准开发技术:同时通过基于策略配置的数据缓存机制,聚合业务数据并发连接不同的后端业务 ...
- Java中String和Int的相互转换
一.将字串 String 转换成整数 intA. 有2个方法:1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([Strin ...
- iOS开发——UI基础-Xcode资源拷贝
#1.拷贝资源的时候选择的copy的含义: 是否要将资源拷贝一份到项目中, 如果不勾选就代表着不拷贝, 那么原来的资源不见了, 项目中的也不能用了 注意: 1.虽然项目中的图片和外部的图片是同一张图片 ...
- ImageView显示网络图片
package com.example.urlimage; import java.io.InputStream; import java.net.HttpURLConnection; import ...
- 剑指Offer 从尾到头打印链表
题目描述 输入一个链表,从尾到头打印链表每个节点的值. 输入描述: 输入为链表的表头 输出描述: 输出为需要打印的“新链表”的表头 思路: 用容器vector,递归到最后一个元素,push_back到 ...
- hdu1536&&hdu3023 SG函数模板及其运用
S-Nim Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status ...
- [ruby on rails] 深入(1) ROR的一次request的响应过程
示意图 即: 1. 浏览器发起请求 2. Routes对请求进行一个url映射,交给对应的Controller来处理 3/4. Contoller从Model中获取数据(或者操作数据) 5. 返回给 ...
- VS无法启动调试:“生成下面的模块时,启用了优化或没有调试信息“
调试项目遇到错误提示,Visual Studio 2010(或VS2008或VS2005)启动调试的时候,弹出提示信息: 生成下面的模块时,启用了优化或没有调试信息: C:\WINDOWS\Micro ...