LeetCode 129 Sum Root to Leaf Numbers
2015-11-30 09:31
393 查看
题目描述
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,
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.
分析
参考:Sum Root to Leaf Numbers代码
[code] public static int sumNumbers(TreeNode root) { return sumNumbers(root, 0); } static int sumNumbers(TreeNode root, int parentval) { if (root == null) { return 0; } int p = parentval * 10 + root.val; if (root.left == null && root.right == null) { return p; } return sumNumbers(root.left, p) + sumNumbers(root.right, p); }
相关文章推荐
- COM组件
- android nfc P2P模式
- 基本语法元素
- Android Fragment中onActivityResult()方法获取不到返回数据或者无响应的解决办法
- Skype for business之Skype会议直播
- H5版本的CCBPM设计器将有哪些变化
- ASP.NET MVC- 在Area里使用RedirectToAction跳转出错的解决方法
- AppStore申请加急审核
- bitmap将头像转换为圆形头像
- Duilib教程-简单介绍
- 尚硅谷:jQuery的替换节点
- 尚硅谷:jQuery的克隆节点
- 【Educational Codeforces Round 2D】【计算几何 圆面积交 模板】Area of Two Circles' Intersection
- jquery的effect-color对easyui没效果.自己冒险写个边框的颜色闪烁.
- 浏览器JS脚本
- HDU 2570 迷障(贪心)
- Qt实现画板部件并和自定义button按钮结合实例
- Android开发之ViewPager使用详解(一)
- 计算机视觉:让冰冷的机器看懂这个多彩的世界
- Swfit中视图跳转