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.

题目大意:给一个二叉树,每个节点都是0-9之间的数字,从根节点到叶结点表示一个数字,求所有的这些数字的和。

解题思路:这道题应该是考察DFS,可以直接DFS求解,或者中序遍历用queue也可以做,最近做题没什么状态,脑袋一团浆糊。

    public int sumNumbers(TreeNode root) {
return sum(root,0);
} public int sum(TreeNode node,int sum){
if(node==null){
return 0;
}
if(node.left==null&&node.right==null){
sum=sum*10+node.val;
return sum;
}
return sum(node.left,sum*10+node.val)+sum(node.right,sum*10+node.val);
}

Sum Root to Leaf Numbers——LeetCode的更多相关文章

  1. Sum Root to Leaf Numbers leetcode java

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

  2. Sum Root to Leaf Numbers [LeetCode]

    Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Basic idea: To store ...

  3. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  4. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  5. LeetCode: Sum Root to Leaf Numbers 解题报告

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  6. 【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 ...

  7. LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II

    1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...

  8. 23. Sum Root to Leaf Numbers

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  9. [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 ...

随机推荐

  1. How to load jars residing over NFS in JBossAS7 classpath ? --reference

    In this article we will discuss how can we load jars residing over NFS in JBoss AS-7 classpath. In s ...

  2. 使用ImageView

    @property (strong, nonatomic) UIPopoverController *pop; //选取图片- (IBAction)selectImage:(UIButton *)se ...

  3. 自定义filter包

    在有些时候,你可能需要以你的所有项目进行全局的过滤. 因为你的项目可以设计到互相的依赖和调用 . 修改在tomcat下的conf下的web.xml文件.和在原来的web-inif下的修改一样,添加fi ...

  4. MySQL中TIMESTAMP和DATETIME区别

    1.两者的存储方式不一样 TIMESTAMP:把客户端插入的时间从当前时区转化为UTC(世界标准时间)进行存储.查询时,将其又转化为客户端当前时区进行返回. DATETIME:不做任何改变,基本上是原 ...

  5. animation的6个属性

    @keyframes 规定动画.   animation 所有动画属性的简写属性,除了 animation-play-state 属性.   animation-name 规定 @keyframes ...

  6. (转)ThinkPHP自定义模板标签详解

    转之--http://www.thinkphp.cn/topic/6258.html 模板标签让网站前台开发更加快速和简单,这让本该由程序猿才能完成的工作,现在只要稍懂得HTM的人也能轻易做到,这也就 ...

  7. Linux 网络配置(固定IP)

    通常linux作为服务器系统时,是不推荐安装图形界面的,因此我们需要掌握非图形界面下如何配置网络,主要两种方式,如下: 一.使用SETUP工具(redhat系列才可以,推荐此修改方式) 1.在命令行直 ...

  8. SQL Server主键自动生成_表and存储过程

    主键表: CREATE TABLE [dbo].[KEYCODE]( [KeyName] [varchar](12) NOT NULL, [KeyTableName] [varchar](40) NU ...

  9. K-means聚类

    聚类算法,无监督学习的范畴,没有明确的类别信息. 给定n个训练样本{x1,x2,x3,...,xn} kmeans算法过程描述如下所示: 1.创建k个点作为起始质心点,c1,c2,...,ck 2.重 ...

  10. HTTP协议学习-02

    HTTP 协议详解之 URL 的组成 http(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于 TCP 的连接方式,HTTP1.1 版本中给出一种持续连接的机制,绝大多数的 ...