【题目】

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.

【题意】

给定一棵二叉树,节点值仅仅可能是[0-9]区间上的值,每一条从根到叶子的节点都能够看成一个整数。现要求把全部路径表示的整数相加,返回和

【思路】

DFS,找到左右的路径,实数化每条路径上组合数。将全部的路径上得到的整数求和。

    

    这里2有个问题:

    1. 假设某条路径太长,组合数已经超出了int的上界怎么办

    2. 假设终于的和超出了int的上界怎么办

    

    题目没有进一步的说明,我们默认所给的測试用例都保证不会越界。

【代码】

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: void dfs(int&result, int num, TreeNode*node){
//result表示全部路径的组合
//num表示根到node的父节点的组合数
if(node){
num=10*num+node->val; //计算从根到当前节点的组合数
if(node->left==NULL && node->right==NULL){
result+=num; //已经找到一个条路径的组合数,累加到result上
return;
} if(node->left) dfs(result, num, node->left);
if(node->right) dfs(result, num, node->right);
}
} int sumNumbers(TreeNode *root){
if(root==NULL)return NULL;
int result=0;
int num=0;
dfs(result, num, root);
return result;
}
};

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

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

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

  3. [leetcode]Sum Root to Leaf Numbers @ Python

    原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...

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

  5. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

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

  6. LeetCode :: Sum Root to Leaf Numbers [tree、dfs]

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

  7. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

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

  8. LeetCode Sum Root to Leaf Numbers(DFS)

    题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...

  9. leetcode Sum Root to Leaf Numbers(所有路径之和)

    转载请注明来自souldak,微博:@evagle 观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的.那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最 ...

随机推荐

  1. mysql查询结果写入文件

    注:转自csdn zuyi532 方法1: shell> mysql -uroot -proot -h localhost xxx库 -e " select * from xxx表 l ...

  2. 虚拟机NAT模式主机ping不通虚拟机解决方案

    本篇没有抓包,只是简单一个实施.需要的童鞋可以拿走这个方法. 虚拟机与真机通信三种模式, 桥接模式,NAT 模式 ,HOST 模式. 桥接就是在真机的网络上模拟一个网卡,给虚拟机申请一个和真机在同一个 ...

  3. 用SCMD2.0.8.0汉化版制作OB地图简易教程

    [综合] [复制链接]     Fenix_king       153 主题 0 好友 1万 积分 金星 该用户从未签到 星币 6392 水晶 0 星望 22 精华 0 发消息 电梯直达 楼主   ...

  4. 关于CSS动画几点要注意的地方

    关于CSS动画几点要注意的地方 js操作transition无效果 先看这个demo以及stackoverflow的问题 http://jsfiddle.net/ThinkingStiff/QNnnQ ...

  5. IC封装

    1.QFN •QFN—Quad Flat No-lead Package 四方无引脚扁平封装 2.SOIC •SOIC—Small Outline IC 小外形IC封装 3.TSSOP •TSSOP— ...

  6. [VC6 console]调用API获取手机归属地

    为了完成作业,就偷个懒糊了个获取手机归属地的程序,.我原本写的是MFC版本的,但是由于MFC的代码不是很通用,加上我没有学MFC的时候看别人MFC代码只能干瞪眼,看不懂,所以便改成控制台版本的了.但这 ...

  7. perl 爬虫两个技巧

    <pre name="code" class="cpp">jrhmpt01:/root/lwp# cat data.html <div cla ...

  8. POJ 2774 Long Long Message(后缀数组)

    [题目链接] http://poj.org/problem?id=2774 [题目大意] 求最长公共子串 [题解] 将两个串中间嵌一个字符相连,求一遍后缀数组 如果排名相邻的两个后缀的开端是分属于两个 ...

  9. MySql事务无法回滚的原因

    使用MySQL时.假设发现事务无法回滚,但Hibernate.Spring.JDBC等配置又没有明显问题时.不要苦恼,先看看MySQL创建的表有没有问题.即表的类型. InnoDB和MyISAM是在使 ...

  10. POJ 3311 Hie with the Pie (BFS+最短路+状态压缩)

    题意:类似于TSP问题,只是每个点可以走多次,求回到起点的最短距离(起点为点0). 分析:状态压缩,先预处理各点之间的最短路,然后sum[i][buff]表示在i点,状态为buff时所耗时...... ...