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. Devexpress TreeList选择父级联动

    Treelist当显示复选框后,父级和子级的复选框没有关联,使用过程中很不便,如图所示 自己给treelist添加父子级联动 /// <summary> /// 初始化TreeList,父 ...

  2. ODBC连接mysql

    配置/etc/odbc.ini 执行命令:isql freeswitch freeswitch 123456 -v 第一个报错: [08S01][unixODBC][MySQL][ODBC 5.3(w ...

  3. eclipse选中变量,相同变量高亮。

    选择Windows->Preferences->Java->Editor->Mark Occurrences,全部选择并保存. 如下图:

  4. HashCheck

    https://github.com/gurnec/HashCheck

  5. 关于STM32库中 __IO 修饰符(volatile修饰符,反复无常的意思)

    STM32例子代码中会有像这样的代码 static __IO uint32_t TimingDelay;  这里边的__IO修饰符不好理解,单从字面可以看出是为IO相关,查其标准库可以得知这个__IO ...

  6. [SAP ABAP开发技术总结]增强Enhancement

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. factory工厂模式之简单工厂SimpleFactory

    简单工厂(Simple Factory) 又叫静态工厂,是工厂模式三中状态中结构最为简单的.1.主要有一个静态方法,用来接受参数,并根据参数来决定返回实现同一接口的不同类的实例.2.或者针对每个产品, ...

  8. iOS - Notification 通知

    1.Notification 通知中心实际上是在程序内部提供了消息广播的一种机制,它允许我们在低程度耦合的情况下,满足控制器与一个任意的对象进行通信的目的.每一个 iOS 程序(即每一个进程)都有一个 ...

  9. iOS - UIImagePickerController

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImagePickerController : UINavigationController <NSCod ...

  10. Android 呼吸灯流程分析

    一.Android呼吸灯Driver实现 1.注册驱动 代码位置:mediatek/kernel/drivers/leds/leds_drv.c 602static struct platform_d ...