Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/

Basic idea: To store the num vector in every node of tree by starting from leaf, the go up util to root.

 class Solution {
public:
vector<vector<int>> subNumbers(TreeNode *root) {
vector<vector<int>> sums;
if(root == NULL)
return sums; if(root->left == NULL && root->right == NULL){
vector<int> seq;
seq.push_back(root->val);
sums.push_back(seq);
return sums;
} vector<vector<int>> left_sums = subNumbers(root -> left);
for(auto item: left_sums) {
item.insert(item.begin(), root->val);
sums.push_back(item);
} vector<vector<int>> right_sums = subNumbers(root -> right);
for(auto item: right_sums) {
item.insert(item.begin(), root->val);
sums.push_back(item);
}
return sums;
} int pow10(int n) {
int ret = ;
for(int i = ; i < n; i++)
ret = ret * ; return ret;
} int sumNumbers(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int sum = ;
vector<vector<int>> sums = subNumbers(root);
for(auto v : sums){
int tmp_sum = ;
for(int i = v.size() - ; i >= ; i -- ) {
tmp_sum += v[i] * pow10(v.size() - - i);
}
sum += tmp_sum;
}
return sum;
}
};

Sum Root to Leaf Numbers [LeetCode]的更多相关文章

  1. Sum Root to Leaf Numbers——LeetCode

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

  2. Sum Root to Leaf Numbers leetcode java

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

  3. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  4. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  5. 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 ...

  6. 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  7. LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II

    1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...

  8. 23. 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 ...

  9. [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 ...

随机推荐

  1. CentOS 7 (无盘安装)PXE服务器的搭建(失败求助版)

    折腾了一天半,PXE无盘服务器以暂时失败而告终. 基本原理 1. 首先客户端主机需要支持PXE,大部分主板都支持. 2. PXE服务器需要安装DHCP.TFTP.FTP服务. 3. DHCP服务用来给 ...

  2. GCC编译器代码优化

    代码优化是指编译器通过分析源代码,找出其中尚未达到最优的部分,然后对其重新进行组合,目的是改善程序的执行性能.GCC提供的代码优化功能非常强大,它通过编译选项-On来控制优化代码的生成,其中n是一个代 ...

  3. [HDOJ1231]最大连续子序列

    混了好几个地方的博客,还是觉得博客园比较靠谱,于是决定在这里安家落户了.本人本科生一个,希望各位巨巨多多指教~ Hello World! 单独一个象征性的问候实在是太low了,还是决定来点实质性的.. ...

  4. 序列的方法(str,list,tuple)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在快速教程中,我们了解了最基本的序列(sequence).回忆一下,序列包含有定值 ...

  5. 概念学习(Concept Learning)

    从特殊的训练样例中归纳出一般函数是机器学习的核心问题.一般函数是对理想目标函数的函数逼近(function approximation).简而言之,从特殊到普通.与此对应的是演绎推理(deductiv ...

  6. iOS - UIWebView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIWebView : UIView <NSCoding, UIScrol ...

  7. sessionKey

    许多人都知道NETSCAPE公司是Internet商业中领先技术的提供者,该公司提供了一种基于RSA和保密密钥的应用于因特网的技术,被称为安全插座层(Secure Sockets Layer,SSL) ...

  8. 操作系统基础知识之————单线程(Thread)与多线程的区别

    单线程(Thread)与多线程的区别 (一)首先了解一下cpu: 随着主频(cpu内核工作时钟频率,表示在CPU内数字脉冲信号震荡的速度,等于外频(系统基本时间)乘倍频)的不断攀升,X86构架的硬件逐 ...

  9. SpringAop学习

    Spring Aop (jdk动态代理和cglib代理) Aop 的概念 aop即面向切面编程,一般解决具有横切面性质的体统(事务,缓存,安全) JDK动态代理: 可以使用实现proxy 类,实现jd ...

  10. ElasticSearch(ES)和solr的关系和区别

    可以参考这篇文章:http://www.cnblogs.com/chowmin/articles/4629220.html Solr 2004年诞生(当时是Solar). ElasticSearch ...