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. 修改数据库表的schema,(表的[dbo.]前缀)

    数据库使用过程中遇到这种问题,请看下图

  2. 正则表达式(/[^0-9]/g,'')中的"/g"是什么意思 ?

    正则表达式(/[^0-9]/g,'')中的"/g"是什么意思 ?     表达式加上参数g之后,表明可以进行全局匹配,注意这里“可以”的含义.我们详细叙述: 1)对于表达式对象的e ...

  3. [51NOD1405] 树的距离之和(树DP)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1405 (1)我们给树规定一个根.假设所有节点编号是0-(n-1 ...

  4. 08.安装Oracle 10g和SQLServer2008(仅作学习使用VirtualBox虚拟机来安装节省电脑资源)

    1.虚拟机和宿主机共享文件夹. 2.右ctrl+F切换VirtualBox全屏 3.安装Oracle 10g 4.输入密码:root------------>下一步 5.勾选网络配置" ...

  5. Maven开发基础总结(Maven自启动,Maven打war包,Maven热部署)

    学习内容: 1.不依赖外部Tomcat,自己启动方式部署 2.Maven打war包,远程部署到centOS 3.Maven热部署(不关闭Tomcat部署应用)   做maven开发前提: 1.编码UT ...

  6. hdu 4123 Bob’s Race 树的直径+rmq+尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  7. ZOJ-2365 Strong Defence 无公共边割边集

    题意:该题的题意晦涩,勉勉强强听别人说了一遍后再读了一遍题才算懂了题意,题图说的是A国因为B国药进攻自己的国家,于是想办法在联通A-B之间的路径上进行阻击.阻击的舰船停留在一个路径上,舰船上都要放置水 ...

  8. jQuery.validate.js+API_cn

      名称 返回类型 描述 validate(options) 返回:Validator 验证所选的FORM valid() 返回:Boolean 检查是否验证通过 rules() 返回:Options ...

  9. Codeforces 713D Animals and Puzzle

    题意:一个n*m的01矩阵,Q个询问,每次询问一个矩形区域内,最大的全1正方形的边长是多少? 题解:dp[0][0][i][j]表示以(i, j)为右下角的正方形的最长边长.RMQ后,二分答案即可. ...

  10. LCM兼容

    1.project-1998-trunk-bootable-bootloader-lk-project:   复制zaw1998aa_platform.mk为zaw2000aa_platform.mk ...