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 ...
随机推荐
- Raid1源代码分析--开篇总述
前段时间由于一些事情耽搁了,最近将raid1方面的各流程整理了一遍.网上和书上,能找到关于MD下的raid1的文档资料比较少.决定开始写一个系列的关于raid1的博客,之前写过的一篇读流程也会在之后加 ...
- JAVA日期处理(Timestamp)
要写一些与数据库连接时的日期处理,pstmt.setDate()的类型是java.sql.Date类型,这种符合规范的类型其实并没有把时分秒存进数据库,所以存取时就应该用setTimestamp()或 ...
- pyqt搜索指定信息 github处找到,谢谢这位朋友的帮助了
def tabunqi(self,text): #第一遍添加之后,不提示,当第二次添加相同的数据时,就提示下 text1=str(text) items = self.downwid ...
- 如何在android上去控制开发进度
这次android的壁纸软件1.0版本终于可以上线了,软件的功能基本上实现了,但是用户体验不太好.在整个开发阶段和测试阶段,出现了很多预料之外的事情,比如size是1M多的json文件解析.高清图片导 ...
- android中使用哪种方式解析XML比較好
SAX是一个用于处理XML事件驱动的"推"模型. 长处是一种解析速度快而且占用内存少的xml解析器,它须要哪些数据再载入和解析哪些内容. 缺点是它不会记录标签的关系.而要让你的应用 ...
- [Redux] Redux: Extracting Container Components -- AddTodo
Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...
- EL 表达式中自己定义函数
第一步: 在WEB-INF/tld/ 文件夹下创建一个func.tld文件例如以下: <taglib xmlns="http://java.sun.com/xml/ns/j2ee&qu ...
- Hacker(18)----了解Windows系统漏洞
一.WinXP中的漏洞 在WinXP中,常见的漏洞主要有UPNP服务漏洞.帮助与支持中心漏洞.压缩文件夹漏洞.服务拒绝漏洞.RDP漏洞以及热键漏洞. 1.UPNP服务漏洞 漏洞描述:UPNP(Univ ...
- oracle数据库连接
///宁采花 8:37:39 /// <summary> /// 获取数据链接 /// </summary> /// <returns></returns&g ...
- 医院设置 codevs 2577
Floyd+二叉树 #include<iostream>#include<cstdlib>#include<cstdio>#include<cstring&g ...