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.
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.
解题思路:
递归,JAVA实现如下:
static public int sumNumbers(TreeNode root) {
if(root==null)
return 0;
return sumNumbers(root,0);
}
public static int sumNumbers(TreeNode root,int res) {
if(root.left==null&&root.right==null)
return res*10+root.val;
if(root.left==null)
return sumNumbers(root.right,res*10+root.val);
if(root.right==null)
return sumNumbers(root.left,res*10+root.val);
return sumNumbers(root.left,res*10+root.val)+sumNumbers(root.right,res*10+root.val);
}
Java for LeetCode 129 Sum Root to Leaf Numbers的更多相关文章
- leetcode 129. Sum Root to Leaf Numbers ----- java
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [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@ [129] Sum Root to Leaf Numbers (DFS)
https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...
- [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#129 Sum Root to Leaf Numbers
原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...
- LeetCode 129. Sum Root to Leaf Numbers 动态演示
树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...
- [LeetCode]129. Sum Root to Leaf Numbers路径数字求和
DFS的标准形式 用一个String记录路径,最后判断到叶子时加到结果上. int res = 0; public int sumNumbers(TreeNode root) { if (root== ...
- 【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 ...
随机推荐
- Jsp2.0自定义标签(第三天)——EL表达式的使用
1.提出问题: 我们经常会看到这样的jsp页面代码: 浏览器显示: 为什么会在页面输出:Hello World ,${per}究竟是如何找到“Hello World”的呢? 2.分析问题: 要想解决 ...
- ios与js交互获取webview元素和赋值
使用webview的stringByEvaluatingJavaScriptFromString的方法交互,直接提供实例. 下载:http://download.csdn.net/detail/hey ...
- 【java】Map、Set、List不同数据结构的各种不同循环迭代的效率对比,使用场景
Map.Set.List不同数据结构的各种不同循环迭代的效率对比,使用场景 引申一个地址:Map迭代的使用keySet和entitySet的效率
- 实现页面切换(动画效果实现,不用ViewPager)
源代码地址 http://download.csdn.net/detail/u013210620/8791687 先看主页面布局activity_main <?xml version=" ...
- 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(一)
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象 1.用户.角色.权限的关系 用户和角 ...
- JAVA Eclipse 出现 load id=gralloc != hmi-id=gralloc怎么办
一般是应用程序权限导致的,在Manifest.xml文件中,targetSdkVersion设置不正确,你可以直接删掉这个信息
- Solidworks如何使用Toolbox
Toolbox不仅仅是智能扣件.事实上,一般常见的轴承,螺栓,齿轮都有了,点击右侧的设计库即可展开Toolbox 配置完成后我只留下一个GB 比如我要选一个圆锥滚子轴承,从右边拖进来即可 ...
- poj 1328 Radar Installation 【贪心】【区间选点问题】
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54798 Accepted: 12 ...
- VueJS自定义过滤器:new Vue({filters:{filter1:function(){}....}})
Vue.js 允许你自定义过滤器,被用作一些常见的文本格式化. 语法 <!-- 在两个大括号中 --> {{ message | capitalize }} <!-- 在 v-bin ...
- Lua学习五----------Lua循环
© 版权声明:本文为博主原创文章,转载请注明出处 1.循环类型 1.1 while循环 - 语法:while(condition) do ...<执行语句> end - 解析:判断cond ...