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. PHP 线程安全与非线程安全版本的区别深入解析

    Windows版的PHP从版本5.2.1开始有Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分,这两者不同在于何处?到底应该用哪种?这里做一个简单的介绍 ...

  2. jQuery属性选择器.attr()和.prop()两种方法

    在判断表单单选框是否被选中时,通常会想到使用$('#checkbox').attr('checked')来判断,但在一些情况下,你会发现这种方法并不管用,得到的是undefined. 原来jQuery ...

  3. Tomcat 6.0 简介

    本片翻译来自:http://tomcat.apache.org/tomcat-6.0-doc/introduction.html 介绍 无论是开发者还是tomcat管理员在使用前都需要了解一些必要的信 ...

  4. Request 传值 遇到的中文乱码问题

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="xxxx.aspx.cs&quo ...

  5. 新手使用R的注意事项

    1.最好先设置工作目录 如: setwd(“D:/DataDig”) 注意不是”\”,是”/” 再读取数据,如: datafile = read.csv("./test.csv") ...

  6. ThinkPHP框架表单验证

    对注册到test表的表单进行验证 在注册之前要对表单进行验证: 用户名非空验证,两次输入密码必须一致即相等验证,年龄在18~50之间即范围验证,邮箱格式正则验证. 自动验证是ThinkPHP模型层提供 ...

  7. Maven编译项目报错:某些类找不到符号

      遇到Maven在编译项目源码时候出现找不到符号错误,主要归结为以下几个问题: 1. 可能项目编码格式不统一. 2. 可能项目编码使用的JDK版本不统一. 项目可能是当前项目也可能是继承的父项目,还 ...

  8. 关于cin,getchar(),scanf()的注意事项(转)

      问题描述一:(分析scanf()和getchar()读取字符) scanf(), getchar()等都是标准输入函数,一般人都会觉得这几个函数非常简单,没什么特殊的.但是有时候却就是因为使用这些 ...

  9. iOS开发——多线程篇——NSOperation(基于GCD多线程编程),下载图片并合成新图片

    一.NSOperation的基本概念1.简介NSOperation的作用配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperatio ...

  10. css3常用标签

    30个最常用css选择器解析   你也许已经掌握了id.class.后台选择器这些基本的css选择器.但这远远不是css的全部.下面向大家系统的解析css中30个最常用的选择器,包括我们最头痛的浏览器 ...