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.


  这题是简单地深度搜索bfs,代码如下:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. /**
  5. * Definition for binary tree
  6. */
  7. struct TreeNode {
  8. int val;
  9. TreeNode *left;
  10. TreeNode *right;
  11. TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  12. };
  13.  
  14. class Solution {
  15. public:
  16. int sumNumbers(TreeNode *root) {
  17. unsigned int cur_val=,ret_val=;
  18. help_f(root,cur_val,ret_val);
  19. return ret_val;
  20. }
  21. void help_f(TreeNode *node,unsigned int &cur_val,unsigned int &ret_val)
  22. {
  23. if(node==NULL) return ;
  24. cur_val=cur_val*+node->val;
  25. help_f(node->left,cur_val,ret_val);
  26. help_f(node->right,cur_val,ret_val);
  27. if(node->left==NULL&&node->right==NULL) ret_val+=cur_val;
  28. cur_val/=;
  29. }
  30. };
  31.  
  32. int main()
  33. {
  34.  
  35. return ;
  36. }

[LeetCode] Sum Root to Leaf Numbers dfs,深度搜索的更多相关文章

  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@ [129] Sum Root to Leaf Numbers (DFS)

    https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...

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

  4. LeetCode Sum Root to Leaf Numbers(DFS)

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

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

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

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

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

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

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

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

随机推荐

  1. 三、Linux 系统目录结构

    Linux 系统目录结构 登录系统后,在当前命令窗口下输入命令:  ls /  你会看到如下图所示: 树状目录结构: 以下是对这些目录的解释: /bin:bin是Binary的缩写, 这个目录存放着最 ...

  2. php v8js

    本文整理自大神 Corz 1.php56 /datas/soft/php56/bin/php -v PHP (cli) #https://blog.csdn.net/lzm198707/article ...

  3. 怎样查看web软件例如apache的连接数

    查看连接总数和当前的连接数 netstat -ant | grep $ip:80 | wc -l netstat -ant | grep $ip:80 | grep EST | wc -l 查看IP访 ...

  4. FLASH、SDRAM

    1.flash: 闪存,掉电之后里面的存储数据不会丢失,在嵌入式系统中用作存储Bootloader以及操作系统或者程序代码或者直接当硬盘使用(U盘).一般主要使用的FLASH有NOR flash和NA ...

  5. 牛课第二次多校I

    链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 题目描述 White Cloud has a square of n*n from (1,1) ...

  6. flex遭遇text-overflow:hidden,white-space:nowrap

    最近在项目中遇到使用flex的时候,在flex-item元素中使用text-overflow:hidden:white-space:nowrap:进行省略文字的操作. 发现flex-item失控了,长 ...

  7. 【Palindrome Partitioning】cpp

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  8. MFC深入浅出读书笔记第二部分2

    第七章  MFC骨干程序 所谓骨干程序就是指有AppWizard生成的MFC程序.如下图的层次关系是程序中常用的几个类,一定要熟记于心. 1 Document/View应用程序 CDocument存放 ...

  9. java面向对象之关键字,权限修饰符

    1.关键字:this,static,package,importthis:1.表示对当前对象的引用!2.表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!其实这是第一种用法的特 ...

  10. TOJ 4689: Sawtooth

    4689: Sawtooth Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 26     ...