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.
本题利用利用深搜到叶子节点,同时记录下sum即可
int dfs(TreeNode *root, int sum){
if(root == NULL) return ;
sum = root->val+sum*;
if(root->left == NULL && root->right == NULL) return sum;
return dfs(root->left,sum)+dfs(root->right,sum);
}
int sumNumbers(TreeNode *root){
if(root == NULL ) return ;
int sum = root->val;
if(root-> left == NULL && root->right ==NULL) return sum;
return dfs(root->left,sum) + dfs(root->right, sum);
}
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 ...
- [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 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(DFS)
题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...
- leetcode Sum Root to Leaf Numbers(所有路径之和)
转载请注明来自souldak,微博:@evagle 观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的.那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最 ...
随机推荐
- JavaWeb学习之什么JSP、JSP是如何工作的、JSP语言(各种指令和九大内置对象)、EL表达式简单使用(5)
1.什么JSP * servlet:java编写的处理动态web的技术 * 特点:Java代码中嵌套html代码 * jsp * 特点:HTMl代码中嵌套java代码 * %tomcat%/conf/ ...
- 在python多进程中使用manager和Barrier
注意:Barrier是PYTHON3才有的功能,在2中无法测试. #!/usr/bin/env python # -*- coding: utf-8 -*- import multiprocessin ...
- SQL Server排序规则
在使用数据库的过程中,总会碰到一些特别的需求.有时候需要储存中文字符,区分大小写或者按照中文的比划顺序排序.这就涉及到了对数据库排列规则的选择. 我们一般可以选择数据库名称-->右键属性(Pro ...
- go sample - hello world
入门级别的hello world package mainimport "fmt"func main() { fmt.Println("blibliblbibl!&quo ...
- angularjs 权威指南 版本 1.2.6
1 $rootScope run : run 方法初始化全局的数据 ,只对全局作用域起作用 如$rootScope <script src="http://apps.bdimg.c ...
- html5 方框内的小球
html5 方框内的小球 版本一 <!DOCTYPE html> <html> <head lang="en"> <meta charse ...
- android 入门-Eclipse 费解的问题
1.第一次打开eclipse的时候 代码程序出好多红点.等待加载项目,如果加载完项目之后仍然存在,请重启eclipse. 2.如果你在创建页面中的button 的时候,设置了android:gravi ...
- hdu 1866 几个矩形面积的和 ***
给出几个矩形,求出面积之和,相交区域只能算一次 一开始想用线段树求个并,后来发现没办法知道并了几次,然后就不会了 看了题解 本题其实很简单,但是想要想到转化也很不容易,因为本题是求面积之和,但是两个矩 ...
- Visual Studio 2015将在7月20号RTM
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:用了3个多月的VS 2015终于要迎来RTM了,不过感觉有点淡淡的忧伤(为什么呢?请看 ...
- 卸载已经安装的rpm包
两个关键点: 1.如果提示有xxx.rpm包已经被installed了,那么先用rpm -e --nodeps xxx来卸载 2.如果存在多个版本的话,用rpm -e --allmatches来卸载 ...