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 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 1:
使用递归完成。我们要做的是 把左右子树的path加起来。
base case: root是叶子节点时,直接计算path。我们用pre保留上一层计算出来的数字的值。那么到了当前根节点应该就是
把上一层的值*10再加上root的值。
/**
* Definition for binary tree
* 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);
} public int dfs(TreeNode root, int pre) {
if (root == null) {
return 0;
} int cur = pre * 10 + root.val;
if (root.left == null && root.right == null) {
return cur;
} return dfs(root.left, cur) + dfs(root.right, cur);
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/SumNumbers_1208_2014.java
LeetCode: 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 解题思路
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 number ...
- [leetcode]Sum Root to Leaf Numbers @ Python
原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...
- 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 [129]
[题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a n ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- LeetCode :: Sum Root to Leaf Numbers [tree、dfs]
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 dfs,深度搜索
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- linux shell 脚本攻略学习1
1.关于echo和printf打印输出 如果要使用转义序列,那么需要在echo 后面加上参数 -e来进行识别,下面例子是进行对比: amosli@amosli-pc:~/learn$ echo -e ...
- 命令行参数解析函数getopt和getopt_long函数【转】
原文地址:http://blog.csdn.net/cashey1991/article/details/7942809 getopt和getopt_long函数 平时在写程序时常常需要对命令行参 ...
- 转:SpringMVC中日期格式的转换
解决日期提交转换异常的问题 由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑定.前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestM ...
- SaltStack入门到精通第一篇:安装SaltStack
SaltStack入门到精通第一篇:安装SaltStack 作者:纳米龙 发布日期:2014-06-09 17:50:36 实际环境的设定: 系统环境: centos6 或centos5 实验机 ...
- 魅族MX四核手机转让,二手淘宝上+hi-pda论坛结合使用成功已出
2013-3-14 内容存档在evernote,笔记名"魅族MX四核手机转让,二手淘宝上+hi-pda论坛结合使用成功已出"
- SQL中获取排序分组后数据的脚本
废话不多说了,先上一段代码,如下所示: select distinct b.OrgID,b.CompanyID,b.AreaID,b.CustChannelID,b.CustID,b.SaleTo ...
- golang 学习 ---- channel
把一个loop放在一个goroutine里跑,我们可以使用关键字go来定义并启动一个goroutine: package main import "fmt" func loop() ...
- 跟我学SharePoint 2013视频培训课程——怎样创建文档库并上传文档(8)
课程简介 第8天,怎样在SharePoint 2013怎样创建文档库并上传文档. 视频 SharePoint 2013 交流群 41032413
- [转]JDBC快速入门教程
JDBC是什么? JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关系数据库中的数据.JDBC代表Java数据库连接. JDBC库中所包含的API任务通常与数据库使用: ...
- 转:zTree树控件入门之checkbox:如何动态设置节点的checkbox选择框启用与禁用状态(chkDisabled)
当一棵树的部分节点根据登入用户角色不同而决定是否启用节点前的checkbox选择框的时候,我们应该如何做呢?也或者如何在页面加载的时候动态根据当前登入用户角色动态切换节点前的checkbox的禁用状态 ...