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. 绕过HR破门而入的求职智慧

    以往我们在网上看到的很多求职文章或指导性纲领,譬如啥自信.做功课.良好形象.华丽的简历.工作经验.口才啥的,其实到了21世纪尤其是互联网高速发展的今天,前面这些技巧就显得无比空洞: 1.因为自信谁都可 ...

  2. 【AngularJS】—— 1 初识AngularJs

    怀着激动与忐忑的心情,开始了学习AngularJS的旅程,很久之前就听说了这个前端框架,但是由于自己一直没有从事相关的工作,因此也没有进行学习.这次正好学习AngularJS,直接复习一下前端的知识. ...

  3. 黄学长模拟day1 球的序列

    N个编号为1-n的球,每个球都有唯一的编号.这些球被排成两种序列,分别为A.B序列,现在需要重新寻找一个球的序列l,对于这个子序列l中任意的两个球,要求j,k(j<k),都要求满足lj在A中位置 ...

  4. plt和got

    最近在学习linux高级调试技术.下面就动态库连接这块做了一个实验 首先理解下plt是procedure linkage table,got是global offset table.got表中存放的是 ...

  5. mybatis 多个dao重名,根据namespace解析

    在mybatis通过执行sql语句的方式是,用getSqlSession().xxx(param,..)方法来调用, 其中第一个参数就是dao mapper.xml文件的命名空间.id package ...

  6. 如何禁用wordpress的RSS Feed

    RSS(Really Simple Syndication)是一种描述和同步网站内容的格式,早期使用RSS订阅能更快地获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新.但随着采集技术的 ...

  7. webpack 教程 那些事儿01-webpack是什么

    文章目录 1. 为什么引入webpack? 2. webpack到底是什么? 3. webpack的工作流程理念 4. webpack的使用 4.1. install webpack 5. 分享源码d ...

  8. C# switch

    要开学了(啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊) 做个沉默的行动者吧!(嘘嘘嘘嘘嘘嘘)今天去水题发现好多基础都不知道啊 1.   switch(控制语句) { case 常量表达式:{statement ...

  9. JQuery的Ajax跨域请求的解决方案

    客户端调用代码示例: var myurl = "http://js.yingdoo.com/embed/CAPTCHA.ashx?m=" + phone_val + "& ...

  10. Activity的四个启动模式

    /** * Activity有四种启动模式(android:launchMode) * 分别是: * 1. standard(默认),可以不停的在栈中创建新的Activity * 2. singleT ...