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. return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public boolean hasPathSum(TreeNode root, int sum) {
  12. boolean flag=false;
  13.  
  14. if(root==null)
  15. return false;
  16.  
  17. if(root.left==null&&root.right==null&&root.val==sum)
  18. {
  19. flag=true;
  20. return flag;
  21. }
  22.  
  23. if(root.left!=null||root.right!=null)
  24. {
  25. if(root.left!=null&&flag==false)
  26. {
  27. flag=hasPathSum(root.left,sum-root.val);
  28. if(flag==true)
  29. return flag;
  30. }
  31.  
  32. if(root.right!=null&&flag==false)
  33. {
  34. flag=hasPathSum(root.right,sum-root.val);
  35. if(flag==true)
  36. return flag;
  37. }
  38.  
  39. }
  40. return flag;
  41. }
  42. }
  1. 4 8
  2. / / \
  3. 11 13 4
  4. / \ \
  5. 7 2 1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

代码如下:

112. Path Sum的更多相关文章

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

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

  3. Leetcode 笔记 112 - Path Sum

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

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

  5. 112. Path Sum二叉树路径和

    [抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

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

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

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

  9. [LeetCode]题解(python):112 Path Sum

    题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...

随机推荐

  1. 用C#用C#实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码

    run(){        while(true)        {                show();                if(条件)                {     ...

  2. bzoj 1965: [Ahoi2005]SHUFFLE 洗牌

    #include<cstdio> #include<cstring> #include<iostream> #define ll long long using n ...

  3. Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use.

    Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. The ...

  4. 如何禁止在DBGRID末位自动添加一行记录

    http://www.tc5u.com/cpp/xg-1730729.htm 用DBGridEh吧,只要设置属性alopAppendEh为false即可 最简单将DBGrid的option属性中goE ...

  5. C++-static的用法

    static成员变量 为什么要有静态成员变量:1)不进入全局名字空间.2)实现信息隐藏 要点: 除了整型的const 静态成员变量可以在类体内初始化,其它值都需要在类体外的实现文件定义 static函 ...

  6. High Performance Django

    构建高性能Django站点   性能 可用 伸缩 扩展 安全 build 1.审慎引入第三方库(是否活跃.是否带入query.是否容易缓存) 2.db:减少query次数 减少耗时query 减小返回 ...

  7. 多线程同步内功心法——PV操作上(未完待续。。。)

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  8. C++二叉查找树实现及转化为双向链表

    二叉树首先要有树节点 template<class T> class BinaryNode { public: T element; BinaryNode *left; BinaryNod ...

  9. python解无忧公主数学题107.py

    python解无忧公主数学题107.py """ python解无忧公主数学题107.py http://mp.weixin.qq.com/s?__biz=MzI5ODE ...

  10. Android开发-略讲adb命令和SQLite数据库运用

    adb.exe  ADB -Android Debug Bridge, 是 Android sdk 里的一个工具,用这个工具可以直接操作管理 Android 模拟器或者真实的 Android 设备 简 ...