【题目】

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. 1
  2. / \
  3. 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的上界怎么办

    

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

【代码】

  1. /**
  2. * Definition for binary tree
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12.  
  13. void dfs(int&result, int num, TreeNode*node){
  14. //result表示全部路径的组合
  15. //num表示根到node的父节点的组合数
  16. if(node){
  17. num=10*num+node->val; //计算从根到当前节点的组合数
  18. if(node->left==NULL && node->right==NULL){
  19. result+=num; //已经找到一个条路径的组合数,累加到result上
  20. return;
  21. }
  22.  
  23. if(node->left) dfs(result, num, node->left);
  24. if(node->right) dfs(result, num, node->right);
  25. }
  26. }
  27.  
  28. int sumNumbers(TreeNode *root){
  29. if(root==NULL)return NULL;
  30. int result=0;
  31. int num=0;
  32. dfs(result, num, root);
  33. return result;
  34. }
  35. };

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. The following classes could not be found: - android.support.v7.internal.widget.ActionBarOverlayLayou解决方案

    如图出现如下的错误的时候,一般都是升级Androdi Studio 后导致的,引入库不全,或者其他 东西缺少,可以如下解决方案: 或者如下的解决方案: 在布局文件的Design界面中,修改原来的App ...

  2. Javascript 设计模式笔记

    设计模式太多了 还有些模式概念非常接近(比如观察者 中介者 和 事件发布/订阅模式) 构造器模式 var newObject = {} var newObject = new XXX(); 模块模式 ...

  3. MySQL 分区表各个分区的行数

    分区的信息是记录在information_schema.partitions 这个表里的.它不能直接定位行所在的分区,但它可查到每个分区中有多少行. 例子: select partition_name ...

  4. JAVA并发,CountDownLatch使用

    该文章转自:http://www.itzhai.com/the-introduction-and-use-of-a-countdownlatch.html CountDownLatch 1.类介绍 一 ...

  5. perspective结合transform的3D效果

    http://css-tricks.com/almanac/properties/p/perspective/ 链接中讲了 perspective的两种用法及比较: 1.perspective:100 ...

  6. 完美解决CTRL+空格不能切换中/英文输入法的问题

    首先任务栏上的输入法图标上点右键选择设置. 然后选择键设置,双击第一个“在不同的输入语言之间切换”先勾选“切换输入语言”下面选择左手ALT.取消右边“切换键盘布局”前的勾. 然后进入“中文(简体)输入 ...

  7. CountDownLatch和CyclicBarrier区别及用法的demo

    javadoc里面的描述是这样的. CountDownLatch: A synchronization aid that allows one or more threads to wait unti ...

  8. 升级automake和autoconf

    <pre name="code" class="html">zjtest7-redis:/root/soft/json-c-json-c-0.12- ...

  9. java字符串函数及理解

    Java中的字符串也是一连串的字符.但是与许多其他的计算机语言将字符串作为字符数组处理不同,Java将字符串作为String类型对象来处理.将字符串作为内置的对象处理允许Java提供十分丰富的功能特性 ...

  10. centos6.5设备mysql5.6

    1. 首先检查版本号number # uname -a 要么 # cat /etc/redhat-release CentOS release 6.6 (Final) 2. 下载并安装Mysql的yu ...