【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 ...
随机推荐
- HTML DOM 属性记录
将HTML DOM中几个容易常用的属性做下记录,需要的朋友可以参考下. nodeName.nodeValue 以及 nodeType 包含有关于节点的信息. nodeName 属性含有某个节点 ...
- python Memo
list&tuple 运算 乘以constant >>> x = ((1,2),) >>> x*2 ((1, 2), (1, 2)) >>> ...
- Kill 正在执行的存储过程
1.找到正在执行的存储过程的 sid ,serial# select b.sid,b.SERIAL#,a.OBJECT, 'alter system kill session ' || ''' ...
- 运用JavaScript构建你的第一个Metro式应用程序(on Windows 8)(一)
原文 http://blog.csdn.net/zhangxin09/article/details/6784547 作者:Chris Sells 译: sp42 原文 包括 HTML.CSS 和 ...
- [Drools]JAVA规则引擎 -- Drools 2
上一篇文章 http://blog.csdn.net/quzishen/archive/2011/01/25/6163012.aspx 描述了一些常用的drools的语法标签和一个模拟实例即发送积分的 ...
- Javascript 装载和执行
http://coolshell.cn/articles/9749.html http://www.cnblogs.com/cheche/archive/2011/03/06/1971955.html
- stack的应用
STL除了给我们提供了一些容器(container)以外,还给我们提供了几个容器适配器(container adapters),stack便是其中之一 看过STL源码的人都知道,stack其实是内部封 ...
- NGINX 多个域名配置
多个域名配置: 依赖于 include 这个功能会加在 这2个文件夹下的所有配置文件. 所以我们可以配置多个 conf 放置于这些文件夹中.这样就是先了多个域名配置 conf 内容大致如下 s ...
- Java并发编程:Thread类的使用介绍
在学习Thread类之前,先介绍与线程相关知识:线程的几种状态.上下文切换,然后接着介绍Thread类中的方法的具体使用. 以下是本文的目录大纲: 一.线程的状态 二.上下文切换 三.Thread类中 ...
- 2014.9.25DOM元素操作
2.操作样式class a.className=”block” class样式,代码赋值的方式 (五)找相关元素 a.nextSibling 下一层,下一个同辈元素 a.previousSibling ...