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 ...
随机推荐
- java unicode转码为中文 实例
package com.infomorrow.parser_report; import org.junit.Test; public class Decode { @Test public void ...
- at com.mysql.jdbc.SQLError.createSQLException
WARN run, com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1de6191 -- APPARENT DEA ...
- IIS7虚拟目录出现HTTP错误500.19(由于权限不足而无法读取配置文件)的解决方案
今天在window7上配置asp.net网站,但是访问总是提示 错误摘要HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该页的相关配置数据无效.详细 ...
- C# 自定义控件,日期时间选择输入插件
权声明:本文为博主原创文章,未经博主允许不得转载. // 为textBox1添加一个日期时间选择控件 DateTimeChoser.AddTo(textBox1); DateTimeChoser.De ...
- RVM Ruby 管理工具
1.RVM 简介 1.1 Ruby 简介 Ruby 是一种面向对象的脚本语言,简单易用,功能强大.能跨平台和可移植性好等等.其实就是种脚本语言. Ruby 的软件源使用的是亚马逊的云服务,国内网络环境 ...
- solr开发从查询结果集中获取对象数据
solrJ从查询结果集中获取对象数据. 方案一:自定义转换方式 /** * * SolrDocument与实体类转换 [测试通过] * * @author pudongping * * @param ...
- 【Linux】字符转换命令join
join 看字面上的意义 (加入/参加) 就可以知道,他是在处理两个文件之间的数据,而且,主要是在处理『两个文件当中,有 "相同数据" 的那一行,才将他加在一起』的意思.我们利用底 ...
- Ios开发中UILocalNotification实现本地通知实现提醒功能
这两天在做一个日程提醒功能,用到了本地通知的功能,记录相关知识如下: 1.本地通知的定义和使用: 本地通知是UILocalNotification的实例,主要有三类属性: scheduled time ...
- How to convert String to Date – Java
In this tutorial, we will show you how to convert a String to java.util.Date. Many Java beginners ar ...
- 五分钟读懂UML类图(转)
平时阅读一些远吗分析类文章或是设计应用架构时没少与UML类图打交道.实际上,UML类图中最常用到的元素五分钟就能掌握,下面赶紧来一起认识一下它吧: 一.类的属性的表示方式 在UML类图中,类使用包含类 ...