题意:

  给一棵二叉树,每个节点上有一个数字,范围是0~9,将从根到叶子的所有数字作为一个串,求所有串的和。

思路:

  普通常规的DFS。

  1. /**
  2. * Definition for a binary tree node.
  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. int sumNumbers(TreeNode* root) {
  13. if(root==NULL) return ;
  14. return DFS(root,);
  15. }
  16. int DFS(TreeNode* t,int sum)
  17. {
  18. sum=sum*+t->val;
  19. if(!t->left && !t->right) return sum;
  20. int left=, right=;
  21. if(t->left) left=DFS(t->left,sum);
  22. if(t->right) right=DFS(t->right,sum);
  23. return left+right;
  24. }
  25. };

AC代码

LeetCode Sum Root to Leaf Numbers(DFS)的更多相关文章

  1. 【leetcode】Sum Root to Leaf Numbers(hard)

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

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

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

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

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

  5. LeetCode OJ: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 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 求根到叶节点数字之和

    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

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

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

随机推荐

  1. Remote Debugging Chrome 结合Genymotion模拟器的移动端web真机调试(转)

    尝试了好多方法,刚开始想用bluestacks调试手机页面,不过在打开chrome的时候,会黑屏,什么也看不了.最后又是更新android系统,又是卸掉bluestacks重新安装,怎么都不行.最后没 ...

  2. Mysql复制表格

    1.复制表结构及数据到新表 CREATE TABLE 新表 as SELECT * FROM 旧表 不过这种方法的一个最不好的地方就是新表中没有了旧表的primary key.Extra(auto_i ...

  3. mysql 索引长度限制

    MyISAM存储引擎引键的长度综合不能超过1000字节 InnoDB单列索引长度不能超过767bytes,联合索引还有一个限制是3072

  4. Java DES 加解密文件

    import com.mchange.v2.io.DirectoryDescentUtils; import javax.crypto.Cipher;import javax.crypto.Ciphe ...

  5. iOS各种动画效果

    ios各种动画效果 最普通动画: //开始动画 [UIView beginAnimations:nil context:nil];  //设定动画持续时间 [UIView setAnimationDu ...

  6. (转载)Java基础知识总结

    写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部分用到哪些语句,方法,和对象. 4,代码实现.用具体的java语言代码把思路体现出来. 学习新技 ...

  7. function gzdecode

    function gzdecode($data) { return gzinflate(substr($data,10,-8)); } 因为项目要下载一个gzip压缩的网页,所以需要解压,手册上有一个 ...

  8. hdu 4616 Game

    http://acm.hdu.edu.cn/showproblem.php?pid=4616 要记录各种状态的段  a[2][4] a[0][j]表示以trap为起点一共有j个trap的最优值 a[1 ...

  9. 数据结构-AVL树

    实现: #ifndef AVL_TREE_H #define AVL_TREE_H #include "dsexceptions.h" #include <iostream& ...

  10. WP8.1 Study5:Data binding数据绑定

    一.数据绑定 最简单的编程UI控件的方法是写自己的数据来获取和设置控件的属性,e.g. , textBox1.Text = "Hello, world"; 但在复杂的应用程序,这样 ...