Sum Root to Leaf Numbers 解答
Question
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.
Solution
基本思路是Recursion。我们可以这样看待问题:对于非叶子结点,我们更改它的数值(prev * 10 + currentNode.val)。然后将所有叶子结点的值相加即得到结果。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int sumNumbers(TreeNode root) {
return dfs(root, 0, 0);
} private int dfs(TreeNode root, int prev, int sum) {
if (root == null) {
return sum;
}
prev = prev * 10 + root.val;
if (root.left == null && root.right == null) {
sum += prev;
return sum;
}
return dfs(root.left, prev, sum) + dfs(root.right, prev, sum);
}
}
Sum Root to Leaf Numbers 解答的更多相关文章
- 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 ...
- 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】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 ...
- 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 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 ...
- [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
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
随机推荐
- 什么是 docker?
关于 Docker 是什么,有个著名的隐喻:集装箱.但是它却起了个“码头工人”( docker 的英文翻译)的名字.这无疑给使用者很多暗示:“快来用吧!用了 Docker ,就像世界出现了集装箱,这样 ...
- poj 2441 Arrange the Bulls(状态压缩dp)
Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to ...
- NetAnalyzer笔记 之 二. 简单的协议分析
[创建时间:2015-08-27 22:15:17] NetAnalyzer下载地址 上篇我们回顾完了NetAnalyzer一些可有可无的历史,在本篇,我决定先不对NetAnalyzer做介绍,而是先 ...
- float position的測试案例
依据<a target=_blank href="http://blog.csdn.net/goodshot/article/details/44348525">htt ...
- Unity 对象池的使用
在游戏开发过程中,我们经常会遇到游戏发布后,测试时玩着玩着明显的感觉到有卡顿现象.出现这种现象的有两个原因:一是游戏优化的不够好或者游戏逻辑本身设计的就有问题,二是手机硬件不行.好吧,对于作为程序员的 ...
- UIActivityIndicatorView-初识IOS
UIActivityIndicatorView是一个加载动画的视图,一般加载一个网页页面之前会经常用到. 上一个随笔,我讲到了页面加载的页面的那些代理方法 - (void) viewWillAppea ...
- 卸载rpm包提示:error: specifies multiple packages
–allmatches Remove all versions of the package which match PACKAGE_NAME. Normally an error is issue ...
- Linux的启动流程
1.首先是bios加电自检.初始化,这个过程会检测相关硬件(cpu.内存.硬盘等),然后会读取硬盘中的MBR:2.加载内核,读取/boot里边的配置文件:3.启动初始化进程,开始运行/sbin/ini ...
- Sqlserver统计语句
--查看被缓存的查询计划 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED st.text AS [SQL] , cp.cacheobjtype , c ...
- (转)SQL语句中的N'xxxx'是什么意思
SQL语句中的N'xxxx'是什么意思 我们在一些sql存储过程,触发器等中经常会见到类似 N'xxxx' 是什么意思? 例如:if exists (select * from dbo.sysobje ...