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遍历这棵树,然后到叶子节点的时候将值加到最 ...
随机推荐
- webservice 简单入门 (NLY)
1,创建webservice服务器端 搭建网站,创建webservice webservice.cs中的代码 namespace WebApplication1 { /// <summary&g ...
- Android性能优化系列 + Android官方培训课程中文版
Android性能优化典范 - 第6季 http://hukai.me/android-performance-patterns-season-6/ Android性能优化典范 - 第5季 htt ...
- Android OkHttp完全解析 --zz
参考文章 https://github.com/square/okhttp http://square.github.io/okhttp/ 泡网OkHttp使用教程 Android OkHttp完全解 ...
- Android中Thread和Service的区别zz
1). Thread:Thread 是程序执行的最小单元,它是分配CPU的基本单位.可以用 Thread 来执行一些异步的操作. 2). Service:Service 是android的一种机制,当 ...
- 【翻译十】java-固定锁和同步
Intrinsic Locks and Synchronization Synchronization is built around an internal entity known as the ...
- 在SSIS包中的事务处理
在处理SSIS包的数据ETL操作过程中,我们经常遇到的一个问题就是一系列步骤在运行的过程中,如果中间的一个步骤失败了,那么我们就需要清理前面已经运行过的步骤所产生的数据或者结果,这往往是一个很头疼的过 ...
- git clean 使用方法
删除 一些 没有 git add 的 文件: git clean 参数 -n 显示 将要 删除的 文件 和 目录 -f 删除 文件,-df 删除 文件 和 目录 git clean -n git c ...
- annotation-config 和 component-scan 的区别
<context:annotation-config> 和 <context:component-scan>是Spring Core里面的两个基础概念,每个使用者都有必要理解怎 ...
- Android 大牛的 blog 值得推荐 (转 整理)
1 收集了 国外著名开发者 25 人,包括 Github 地址.Blog 地址以及重点贡献介绍 链接 收集了 国内部分开发者 32人,包括 Github 地址.Blog 地址以及重点贡献介绍, 链接 ...
- 同一个项目,项目名称不一致,这两个项目同时在Eclipse中出现
在Eclispse中,实际同一个项目,项目名称不一致,这两个项目同时在Eclipse中出现. ①打开项目文件夹,找到“.cproject”文件 ② 在<name>节点重命名 ③ 导入Ecl ...