Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,

  1. 5
  2. / \
  3. 4 8
  4. / / \
  5. 11 13 4
  6. / \ \
  7. 7 2 1
  1. struct TreeNode {
  2. int val;
  3. TreeNode *left;
  4. TreeNode *right;
  5. TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  6. };
  7. class Solution {
  8. public:
  9. bool hasPathSum(TreeNode *root, int sum) {
  10. // Start typing your C/C++ solution below
  11. // DO NOT write int main() function
  12. if(!root) return false;
  13. flag = false;
  14. target = sum;
  15. preOrder(root,);
  16. return flag;
  17.  
  18. }
  19. void preOrder(TreeNode* node,int sum){
  20. sum = node->val + sum; //递归前,加上当前节点
  21. if(node->left)
  22. {
  23. preOrder(node->left,sum);
  24. }
  25. if(node->right)
  26. {
  27. preOrder(node->right,sum);
  28. }
  29. if(!node->left && !node->right && sum == target) //递归结束条件:到了叶子节点
  30. {
  31. flag = true;
  32. }
  33. }
  34. private:
  35. bool flag;
  36. int target;
  37. };

112. Path Sum (Tree; DFS)的更多相关文章

  1. 124. Binary Tree Maximum Path Sum (Tree; DFS)

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

  2. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  3. [LeetCode] 112. Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  4. LeetCode OJ 112. Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  5. [LeetCode] 112. Path Sum_Easy tag: DFS

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  6. LeetCode 112 Path Sum(路径和)(BT、DP)(*)

    翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...

  7. [LeetCode] 112. Path Sum 路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  8. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  9. [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)

    Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...

随机推荐

  1. 如何优雅的使用RabbitMQ(转)

    RabbitMQ无疑是目前最流行的消息队列之一,对各种语言环境的支持也很丰富,作为一个.NET developer有必要学习和了解这一工具.消息队列的使用场景大概有3种: 1.系统集成,分布式系统的设 ...

  2. Mac环境下终端(Terminal)用ssh 连接服务器问题 Received disconnect from 120.55.x.x: 2: Too many authentication failures for root

    由于这台Mac配置git生成公钥后,ssh连接就出现来这个问题 Received disconnect from 120.55.x.x: 2: Too many authentication fail ...

  3. redis+php实现微博功能(二)

    数据结构: set post:postid:3:time timestampset post:postid:3:userid 5 set post:postid:3:content 测试发布哈哈哈哈 ...

  4. WPF ComboBox下拉绑定Treeview 功能的实现

    因为项目需要,接触到这个功能点,借助网络还有自己的一点摸索,实现了这个功能.相关代码如下: XAML部分的代码: <ComboBox Grid.Row=" RenderTransfor ...

  5. Makefile编写 五 隐含规则

    隐含规则———— 在我们使用Makefile时,有一些我们会经常使用,而且使用频率非常高的东西,比如,我们编译C/C++的源程序为中间目标文件(Unix下是[.o]文件,Windows下是[.obj] ...

  6. Linux之安装软件

    1.  下载获得redis-3.0.4.tar.gz后将它放入我们的Linux目录/opt 2. 在SecureCRT界面上点SecureFX图标 在本地窗口中找到要上传的文件 在要上传的文件上点右键 ...

  7. LINK : warning LNK4098: 默认库“LIBCMTD”与其他库的使用冲突;请使用 /NODEFAULTLIB:library

    LINK : warning LNK4098: 默认库“LIBCMTD”与其他库的使用冲突:请使用 /NODEFAULTLIB:library 转自:http://blog.csdn.net/pgms ...

  8. SharedPreference工具类

    public class SPUtils { /** * 保存在手机里的SP文件名 */ public static final String FILE_NAME = "my_sp" ...

  9. Java-Runoob-面向对象:Java 抽象类

    ylbtech-Java-Runoob-面向对象:Java 抽象类 1.返回顶部 1. Java 抽象类 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的 ...

  10. Hive使用druid做连接池代码实现

    配置文档 hive_jdbc_url=jdbc:hive2://192.168.0.22:10000/default hive.dbname=xxxxx hive_jdbc_username=root ...