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.

思路:最近睡得不好,搞得脑子也短路了。一直想着怎么从叶节点往上获取数字。费了好大的劲才AC了,结果一看答案,立马郁闷了。不到10行就能搞定。

具体的原理是深度优先,在向下迭代的过程中,用一个变量不断的把父节点以上的数字传下来,返回的时候把结果加起来。

public int sumNumbers(TreeNode root) {
return helper(root, );
} public int helper(TreeNode root, int curSum) {
if(root == null) return ;
curSum = curSum* + root.val;
if(root.left == null && root.right == null) return curSum;
return helper(root.left, curSum) + helper(root.right, curSum);
}

下面是我自己AC的代码,超长,超繁琐,当个反面例子吧。思路是把每个数字都存在一个vector里面。之所以繁琐是因为我只是在返回的时候下功夫。

int sumNumbers(TreeNode *root) {
int s, e, sum = ;
vector<vector<int>> v;
Numbers(root, s, e, v);
for(vector<vector<int>>::iterator it = v.begin(); it != v.end(); ++it)
{
int num = ;
while(!it->empty())
{
num = num * + it->back();
it->pop_back();
}
sum += num;
}
return sum;
} void Numbers(TreeNode * root, int &s, int &e, vector<vector<int>>& v)
{
int sl = -, el = -, sr = -, er = -;
s = e = -;
if(root == NULL) return;
if(root->left == NULL && root->right == NULL) //在叶节点 压入新的数字
{
e = s = v.size();
v.push_back(vector<int>(,root->val));
return;
}
if(root->left != NULL)
{
Numbers(root->left, sl, el, v);
for(int i = sl; i <= el; ++i)
{
v[i].push_back(root->val);
}
s = sl; e = el;
}
if(root->right != NULL)
{
Numbers(root->right, sr, er, v);
for(int i = sr; i <= er; ++i)
{
v[i].push_back(root->val);
}
s = (s == -) ? sr : s;
e = er;
}
}

【leetcode】Sum Root to Leaf Numbers(hard)的更多相关文章

  1. 【LeetCode】Sum Root to Leaf Numbers

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

  2. 【Leetcode】【Medium】Sum Root to Leaf Numbers (未完成)

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

  3. 【树】Sum Root to Leaf Numbers

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

  4. LeetCode Sum Root to Leaf Numbers(DFS)

    题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...

  5. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  6. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  7. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  8. 【LeetCode】456. 132 Pattern 解题报告(Python)

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  9. 【LeetCode】390. Elimination Game 解题报告(Python)

    [LeetCode]390. Elimination Game 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/elimina ...

随机推荐

  1. JS keycode 事件响应

    <script language="javascript"> function keyevent(){ if(event.keyCode==13) alert(&quo ...

  2. PYTHONPATH 可以跨版本 方便使用 (本文为windows方法)转~

    PYTHONPATH是Python搜索路径,默认我们import的模块都会从PYTHONPATH里面寻找. 使用下面的代码可以打印PYTHONPATH: print(os.sys.path) 我的某个 ...

  3. webpack 教程 那些事儿03-webpack两大精华插件,热加载

    本节主要讲述 webpack的两大经典开发调试插件,热插拔内存缓存机制 文章目录 1. html-webpack-plugin插件的使用 2. webpack-dev-middleware 插件登场 ...

  4. android socket编程用Bufferreader读取的一个失败教训

    由于我的手机需要用笔记本开的wifi,躺在床上玩手机时需要关电脑或者是要让电脑放歌的时候总是不想下床,于是我想能不能用一个APP,然后通过局域网实现在手机上对电脑进行操控呢?说干就干. 我在电脑上用的 ...

  5. HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5122 解题报告:定义一种排序算法,每一轮可以随机找一个数,把这个数与后面的比这个数小的交换,一直往后判 ...

  6. HDU 5073 Galaxy(2014鞍山赛区现场赛D题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5073 解题报告:在一条直线上有n颗星星,一开始这n颗星星绕着重心转,现在我们可以把其中的任意k颗星星移 ...

  7. iOS开发——高级篇——地图 MapKit

    一.简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用中,都用到了定位 ...

  8. iOS开发——UI基础-UIScrollView

    一.UIScrollView使用的步骤 1.创建UIScrollView 2.将需要展示的内容添加到UIScrollView中 3.设置UIScrollView的滚动范围 (contentSize) ...

  9. 迟来的Android的Camera开发总结

    这是好久前写的项目,但一直没有去总结.刚好在准备找工作这段时间来总结自己做过的东西,学到的东西. 写Android的自定义的相机应用时,首先要知道一些Camera开发必须知道的尺寸,不然在调试的时候, ...

  10. Android中对Log日志文件的分析[转]

    一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...