【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.
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.
解答
用树的递归解决
/**
* 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) {
if(root==null){
return 0;
}
return sum(root,0);
} public int sum(TreeNode node,int parentVal){
int sum=0;
int temp=parentVal*10+node.val;
if(node.left==null&&node.right==null){
sum=temp;
}
if(node.left!=null){
sum+=sum(node.left,temp);
}
if(node.right!=null){
sum+=sum(node.right,temp);
}
return sum;
}
}
---EOF---
【LeetCode】Sum Root to Leaf Numbers的更多相关文章
- 【leetcode】Sum Root to Leaf Numbers(hard)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【Leetcode】【Medium】Sum Root to Leaf Numbers (未完成)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【树】Sum Root to Leaf Numbers
题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...
- 【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] 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 ----- 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 ...
- 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 ...
随机推荐
- raphael 支持group(简)
raphael 不支持group,里面有的set方法,只是把对象数组存起来,方法调用的时候,遍历都调用下,但是在实际需求上面感觉group还是瞒有用处的,可以控制group下面的节点的交互 比如地图区 ...
- php对xml的处理
$paymentResult = $ips='<Ips><GateWayRsp><head><ReferenceID></ReferenceID ...
- python3.5之输出HTML实体字符
出 关① 徐兰 凭山俯海古边州, 旆②影风翻见戍楼. 马后桃花马前雪,出关争得不回头? [注]关,指居庸关.②旆(pèi),旌旗. 刚刚学习用python写爬虫,实战一下. 抓取出一个网页的内容 ...
- python----设置默认编码
问题:python的默认编码是ascii.在处理中文的时候可能会出现乱码的情况:这个时候我们就需要把编码设置为对应的编码了. 解决方案: 对python文件的头部做如下修改 import sys re ...
- 请求(Request)的参数(Parameter)里包含特殊字符(#等)的正确处理方式
遇到一个问题 在一个地址链接(URL)里使用 url?param1=val1¶m2=val2 的方式传递参数,结果在获取参数值时发现不是当初设定的值. 具体案例 以特殊字符井号(#)为 ...
- [问题解决] "Nautilus could not create the required folder "/home/kenneth/.config/nautilus"
错误: "Nautilus could not create the required folder "/home/kenneth/.config/nautilus" 发 ...
- ARM Cortex M3(V7-M架构)硬件启动程序 二
解析 STM32 的启动过程 解析STM32的启动过程 当前的嵌入式应用程序开发过程里,并且C语言成为了绝大部分场合的最佳选择.如此一来main函数似乎成为了理所当然的起点——因为C程序往往从main ...
- JAVA基础 (二)反射 深入解析反射机制
在谈论到反射这个问题时,你是否有例如以下疑问? 不管是在.NET还是Java中反射的原理和机制是一样的,理解了一种还有一种就能够迎刃而解,想要理解反射首先须要了解底层的一些概念和执行.理解了反射有助于 ...
- 编tuxedo遇到服务问题
各种错误的程序报构建服务: 1. 配置为执行环境变量tmboot –y启动管理流程和服务流程 2. 每日班似这个错误:buildserv:error while loading shared li ...
- onclick=‘’return false“
文章来自 http://www.cnblogs.com/hellen-li/archive/2010/10/22/1858422.html checkbox没有readOnly属性,可以这样让它保持 ...