LeetCode: Sum Root to Leaf Numbers [129]
【题目】
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]的更多相关文章
- 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 ...
- [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 ...
- [leetcode]Sum Root to Leaf Numbers @ Python
原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...
- 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 ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- 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 ...
- [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 ...
- LeetCode Sum Root to Leaf Numbers(DFS)
题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...
- leetcode Sum Root to Leaf Numbers(所有路径之和)
转载请注明来自souldak,微博:@evagle 观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的.那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最 ...
随机推荐
- Hadoop学习之Hadoop集群搭建
1.检查网络状况 Dos命令:ping ip地址,同时,在Linux下通过命令:ifconfig可以查看ip信息2.修改虚拟机的ip地址 打开linux网络连接,在桌面右上角,然后编辑ip地址, ...
- nodejs事件机制
var EventEmitter = function() { this.evts = {}; }; EventEmitter.prototype = { constructor: EventEmit ...
- java中的四则运算
代码的思路是通过正则判断计算每个最小的计算单元.以下是代码: package cn.com.lawchat.forpublicmvc.util; import java.math.BigDecimal ...
- 多版本jQuery的使用剖析
</div> </div> <!-- basic scripts --> <!--[if !IE]> --> <!-- <![endi ...
- oralce dubugs
1,The listener supports no services 2,invalid specification for system parameter LOCAL_LISTENER crea ...
- openstack windows 2008 img
1,制作镜像主机pre Env yum -y install qemu-img virt-install libvirt 2,配置bridge
- HDU1069:Monkey and Banana(DP+贪心)
Problem Description A group of researchers are designing an experiment to test the IQ of a monkey. T ...
- sql的强大功能(看一条sql解决的复杂业务)
一条sql语句解决的复杂业务,请往下看: 业务介绍:一个单位有多个立项(立项信息表里有单位id),每个立项可能被预警多次(预警信息表里的uuid字段的值里包含有立项id或单位id),每 ...
- Database(Mysql)发版控制二
author:skate time:2014/08/18 Database(Mysql)发版控制 The Liquibase Tool related Database 一.Installation ...
- HDU1200:To and Fro
Problem Description Mo and Larry have devised a way of encrypting messages. They first decide secret ...