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 ...
随机推荐
- flannel无法跨主机ping通容器的解决方式
前几天,出现了无法跨主机ping通容器的情况,导致一个node机网络中断,无法访问,排查过程如下. 首先确认,宿主机node2是可以ping通容器 [root@node2 ~]# ping 10.1. ...
- 【GLSL教程】(七)逐像素的光照 【转】
http://blog.csdn.net/racehorse/article/details/6662540 逐像素的方向光(Directional Light per Pixel) 这一节将把前面的 ...
- hibernate save update merge 区别
1.save save的对象是临时对象,首先将对象写入缓存,使其变为持久对象. save函数底层使用的是Insert语句,因此如果数据库中已经存在相同ID的记录,那么会报错 2.update upda ...
- 【开发总结】—— BABYLON 3D开发常见问题及解决方法
前言:组内同事们根据长时间的Babylon.js开发实践,一起将项目开发中遇到的问题及解决方法做了一个梳理. ios [最好] 关闭离线缓存—— 解决添加了反射的mesh 丢失的问题 不要使用 pos ...
- dubbo zookeeper案例
Alibaba有好几个分布式框架,主要有:进行远程调用(类似于RMI的这种远程调用)的(dubbo.hsf),jms消息服务(napoli.notify),KV数据库(tair)等.这个框架/工具/产 ...
- HDU 1017 A Mathematical Curiosity (枚举水题)
Problem Description Given two integers n and m, count the number of pairs of integers (a,b) such tha ...
- TSRC挑战赛:WAF之SQL注入绕过挑战实录
转自腾讯 博文作者:TSRC白帽子 发布日期:2014-09-03 阅读次数:1338 博文内容: 博文作者:lol [TSRC 白帽子] 第二作者:Conqu3r.花开若相惜 来自团队:[Pax.M ...
- 【Excle数据透视】如何升序排列字段列表中的字段
数据透视表创建完毕,那么如何将字段列表中的字段修改为升序排列呢? 解决方案 更改"字段列表"为按"升序"排列 步骤 单击数据透视表任意单元格→右键单击→数据透视 ...
- MFC的运行机理分析+ASC码问题
Win32程序是从WinMain开始执行的,但是MFC把WinMain给封装了,不会出现在你的代码里面,他已经编译到mfc80.dll了(VS2005)了. 在MFC中的执行顺序是这样的,首先Wind ...
- vs调试,监控变量