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. SQL存储过程来调用webservice

    如果用存储过程来调用webservice 那存储过程的功能感觉能做好多事情了? 别自欺欺人了.那些功能还是webservice来实现的... 完整的webservice代码:(也是默认的,新建.asm ...

  2. R语言 常见模型

    转自 雪晴网 [R]如何确定最适合数据集的机器学习算法 抽查(Spot checking)机器学习算法是指如何找出最适合于给定数据集的算法模型.本文中我将介绍八个常用于抽查的机器学习算法,文中还包括各 ...

  3. 示波器trigger的使用方法

    背景: 下位机有俩个IO口设置为外部中断——边沿触发.低电平有效.因此我需要抓取下降沿波形,但低电平时间很短,手动暂停抓取不仅不科学还费力,那么该如何准确的抓取到呢?最好的办法是使用示波器的trige ...

  4. OC第二节 —— NSString和NSMutableString

    1.为什么需要NSString对象        答:在OC中创建字符串时,一般不使用C的方法,    因为C将字符串作为字符数组,所以在操作时会有很多不方便的地方,    在Cocoa中NSStri ...

  5. 牡丹江.2014k(构造)

    K - Known Notation Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Su ...

  6. Hadoop 面试题之Hbase

    Hadoop 面试题之九 16.Hbase 的rowkey 怎么创建比较好?列族怎么创建比较好? 答: 19.Hbase 内部是什么机制? 答: 73.hbase 写数据的原理是什么? 答: 75.h ...

  7. java 1.7

    http://superuser.com/questions/740064/how-to-install-java-1-7-runtime-on-macos-10-9-mavericks sudo r ...

  8. 如何在CentOS 7上安装EPEL源

    EPEL 是什么? EPEL (Extra Packages for Enterprise Linux,企业版Linux的额外软件包) 是Fedora小组维护的一个软件仓库项目,为RHEL/CentO ...

  9. Ubuntu 下apache2开启rewrite隐藏index.php

    为了实现 http://www.example.com/route/route 而不是 http://www.example.com/index.php/route/route 需要开启apache2 ...

  10. H5案例分享:html5重力感应事件

    html5重力感应事件 一.手机重力感应图形分析 1.设备围绕z轴的旋转角度为α,α角度的取值范围在[0,360). 设备在初始位置,与地球(XYZ)和身体(XYZ)某个位置对齐. 设备围绕z轴的旋转 ...