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 ...
随机推荐
- redis(五)redis与Mybatis的无缝整合让MyBatis透明的管理缓存二
在上一篇文中的Cahe类存在各种问题如:一直使用同一个连接,每次都创建新的Cache,项目中老是爆出connection timeout 的异常,存储的key过长等等一系列的问题,解决问题最好的办法就 ...
- 让Solr返回JSON数据
http://localhost:1985/solr/select/?q=*%3A*&version=2.2&start=0&rows=10&indent=on& ...
- setitimer()函数使用
setitimer()为Linux的API,并非C语言的Standard Library,setitimer()有两个功能,一是指定一段时间后,才执行某个function,二是每间格一段时间就执行某个 ...
- 基于visual Studio2013解决算法导论之046广度优先搜索
题目 广度优先搜索 解决代码及点评 // 图的邻接表表示.cpp : 定义控制台应用程序的入口点. // #include <iostream> #include <stac ...
- 侧滑UI
1.视图 activity_main.xml <com.zyhui.cehua.SlidingMenu xmlns:android="http://schemas.android.co ...
- 安装apk程序进android
package com.example.installmyapk; import java.io.File; import android.net.Uri; import android.os.Bun ...
- BZOJ 1057: [ZJOI2007]棋盘制作( dp + 悬线法 )
对于第一问, 简单的dp. f(i, j)表示以(i, j)为左上角的最大正方形, f(i, j) = min( f(i + 1, j), f(i, j + 1), f(i + 1, j + 1)) ...
- android电话接通状态下,关机铃声无法从外放输出
AudioMTKPolicyManager.cpp的startOutput方法中.将在newDevic获取到的后面加入: if(stream==AudioSystem::BOOT)newDevice| ...
- fcntl()
fcntl() F_GETFL--------------------------------------------- 将文件状态标志作为函数值返回. 文件状态标志: ...
- C++ 左值 右值
最近在研究C++ 左值 右值,搬运.收集了一些别人的资料,供自己记录和学习,若以后看到了更好的解释,会继续补充.(打“?”是我自己不明白的地方 ) 参考:<Boost程序库探秘——深度解析C ...