Sum Root to Leaf Numbers leetcode java
题目:
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.
题解:
分析这道题,由根节点往叶节点方向走,就是从高位往地位加和的方向。也就是说,当遍历的节点往叶节点方向走一层的时候,该节点的值应为父节点的值*10+当前节点的值。
由此可以写出代码:
1 int sumhelper(TreeNode root, int levelBase) {
2 if(root == null)
3 return 0;
4
5 if(root.left == null && root.right == null) {
6 return levelBase + root.val;
7 }
8
9 int nextLevelBase = (levelBase + root.val)*10 ;
int leftSubTreeSum = sumhelper(root.left, nextLevelBase);
int rightSubTreeSum = sumhelper(root.right, nextLevelBase);
return leftSubTreeSum + rightSubTreeSum;
}
public int sumNumbers(TreeNode root) {
return sumhelper(root,0);
}
Sum Root to Leaf Numbers leetcode java的更多相关文章
- Sum Root to Leaf Numbers——LeetCode
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- Sum Root to Leaf Numbers [LeetCode]
Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Basic idea: To store ...
- 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之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-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解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II
1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...
- 23. 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 ...
- Java for 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 ...
随机推荐
- HTTP协议-MIME类型
每一个 URL 都代表着一个资源对象,而当我们请求一个网页的时候,看似只请求了一个 URI(统一资源标识符),实际上这个网页可能包含多个 URI,例如图片资源的 URI 和视频资源的 URI 等.此时 ...
- java 使用grpc步骤
1.配置grpc maven依赖 <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-ne ...
- 【BZOJ 4569】 4569: [Scoi2016]萌萌哒 (倍增+并查集)
4569: [Scoi2016]萌萌哒 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 865 Solved: 414 Description 一个长 ...
- BZOJ.1923.[SDOI2010]外星千足虫(高斯消元 异或方程组 bitset)
题目链接 m个方程,n个未知量,求解异或方程组. 复杂度比较高,需要借助bitset压位. 感觉自己以前写的(异或)高斯消元是假的..而且黄学长的写法都不需要回代. //1100kb 324ms #i ...
- BZOJ3956: Count
Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N<=3*10^ ...
- 移动前端开发和 Web 前端开发的区别是什么
可以分成两部分理解1.服务器端开发,也叫后台开发,这是唯一的,对应不同的平台,他负责数据的分发与存储,和一些逻辑的处理.逻辑处理的多少由业务的复杂程度决定.服务端相对独立,与平台没啥关系. 2..1中 ...
- Dell PowerEdge R710服务器内存条插法/Dell 11G/12G系列服务器内存条插法(转)
说明:以我的经验,其实插3/6/9这个顺序去一定没有错. DELL PowerEdge R710服务器支持 DDR3的 DIMM (RDIMM) 或 ECC非缓冲的 DIMM(UDIMM).单列和双列 ...
- 打印 Go 结构体(struct)信息:fmt.Printf("%+v", user)
package main import "fmt" // 用户 type User struct { Id int Name string Age int } func main( ...
- [SQL ERROR 800]Corresponding types must be compatible in CASE expression.
SQL应用报错800.Corresponding types must be compatible in CASE expression. 错误描述: 11:00:51 [SELECT - 0 ro ...
- 华为正在力挺的NB-IoT是什么鬼! - 全文
NB-IoT,Niubility Internet of Thing,即牛掰的物联网技术. 关于物联网,小编想从2款很有趣的应用说起. 这不是在播限制级.这是Nake Labs推出的3D健身镜,这款智 ...