leetcode Sum Root to Leaf Numbers(所有路径之和)
观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的。那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最终结果上就OK了。思路非常之简单就不详述了。直接上代码:
class Solution {
public:
int sumNumbers(TreeNode *root) {
if(root==NULL)
return 0;
int total = 0;
dfs(root,0,total);
return total;
}
void dfs(TreeNode *root,int sum,int& total){
if(root==NULL)
return;
if(root->left==NULL&&root->right==NULL){
total += sum*10+root->val;
}else{..
dfs(root->left,sum*10+root->val,total);
dfs(root->right,sum*10+root->val,total);
}
}
};
leetcode Sum Root to Leaf Numbers(所有路径之和)的更多相关文章
- 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 ...
- [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]Sum Root to Leaf Numbers @ Python
原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...
- LeetCode: Sum Root to Leaf Numbers [129]
[题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a n ...
- 129. Sum Root to Leaf Numbers pathsum路径求和
[抄题]: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- LeetCode :: Sum Root to Leaf Numbers [tree、dfs]
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 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] Sum Root to Leaf Numbers dfs,深度搜索
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- 【HTTP 2】简介(Introduction)
前情提要 在上一篇文章<[HTTP 2.0] 序言>中,我们简要介绍了 HTTP 2 协议的概要和协议状态. 在本篇文章中,我们将会了解到 HTTP 2 协议简介(Introduction ...
- 九度OnlineJudge之1022:游船出租
题目描述: 现有公园游船租赁处请你编写一个租船管理系统.当游客租船时,管理员输入船号并按下S键,系统开始计时:当游客还船时,管理员输入船号并按下E键,系统结束计时.船号为不超过100的正整数. ...
- android项目中刷新activity界面
android项目中在sqlite数据库插入/更新/删除数据后: 1. 刷新当前activity界面数据(手动刷新): 在activity类下新增一个refresh()方法: /** * 刷新, 这样 ...
- 12 - 多线程、执行队列、GCD
一.多线程 进程:一个应用程序配套一个进程,进程会加载应用程序的资源,进程是放代码的,一个进程默认是一个线程(主线程),可以有多个线程 线程:执行代码的是线程,一个线程同时只能读取一段代码 栈里的变量 ...
- TimeUnit
转http://blog.csdn.net/hudashi/article/details/6936604 public enum TimeUnitextends Enum<TimeUnit&g ...
- iTextSharp - 建立PDF文件
原文 iTextSharp - 建立PDF文件 01 using iTextSharp.text; 02 using iTextSharp.text.pdf; 03 ... 04 private vo ...
- img src某个php文件输出图片(回复更改图片readfile读取图片等)
在论坛我们经常看到一回复图片就更改等,这功能是怎么实现的呢,其实更验证码道理相同. 新建文件 randimage.php 加入以下代码: <?php $dir='../../images/'; ...
- C语言数据结构----栈的定义及实现
本节主要说的是数据结构中的栈的基本定义和实现的方式,其中实现的方式采用的是复用顺序表和单向链表的方式. 一.栈的基本定义 1.栈是一种特殊的线性表,只能从固定的方向进出,而且栈进出的基本原则是:先进栈 ...
- 创建Activity
创建Activity 创建 Activity 分为3个步骤: 1.创建一个扩展子Activity的class 2.创建一个Layout 3.在 AndroidMainfest 中 配置这个Activ ...
- PHP:根据IP地址获取所在城市
文件目录: ipLocation -----qqwry ----------QQWry.Dat -----ipCity.class.php ipCity.class.php文件代码: <?php ...