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,

              5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
boolean flag=false; if(root==null)
return false; if(root.left==null&&root.right==null&&root.val==sum)
{
flag=true;
return flag;
} if(root.left!=null||root.right!=null)
{
if(root.left!=null&&flag==false)
{
flag=hasPathSum(root.left,sum-root.val);
if(flag==true)
return flag;
} if(root.right!=null&&flag==false)
{
flag=hasPathSum(root.right,sum-root.val);
if(flag==true)
return flag;
} }
return flag;
}
}
            4   8
/ / \
11 13 4
/ \ \
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. TopCoder SRM 583 TurnOnLamps

    读错题了有没有呀,原来 lamps 是在边上的呀,当成是在点上的了,无语. 直接一个dfs 就可以 从叶子节点开始,如果有必要转换 lamp 的状态则加一个仅包含 这个 lamp 的段 然后向上扩展, ...

  2. JMeter常用函数整理

    "_csvRead"函数 csvRead函数是从外部读取参数,csvRead函数可以从一个文件中读取多个参数. 下面具体讲一下如何使用csvread函数: 1.新建一个csv或者t ...

  3. 二模 (2) day1

    第一题: 题目描述:淘汰赛制是一种极其残酷的比赛制度.2n名选手分别标号1,2,3,…,2n-1,2n,他们将要参加n轮的激烈角逐.每一轮中,将所有参加该轮的选手按标号从小到大排序后,第1位与第2位比 ...

  4. uboot启动内核(3)

    nand read.jffs2 0x30007FC0 kernel; 从NAND读出内核:从哪读,从kernel分区 放到哪去   -0x30007FC0 nand read.jffs2 0x3000 ...

  5. Validform自定义提示效果-使用自定义弹出框

    $(function(){ $.Tipmsg.r=null; $("#add").Validform({ tiptype:function(msg){ layer.msg(msg) ...

  6. CSSOM视图模式(CSSOM View Module)相关整理(转载)

    原文地址 http://www.zhangxinxu.com/wordpress/?p=1907 一.Window视图属性 这些属性可以hold住整个浏览器窗体大小.微软则将这些API称为“Scree ...

  7. Discuz 论坛的搭建(五)

    配置discus论坛 1.下载discus论坛代码 2.解压缩到ApacheProject目录下 3.把discuz的upload文件copy到discuz文件夹下,然后删除upload文件夹 4.修 ...

  8. 系统的 host文件的作用

    有些用户可能已经注意到,我们在上网时除了可使用常规的 http://www.xxx.com或http://www.xxx.com.cn等形式的网站域名之外,还可以使用类似于“202.106.184.2 ...

  9. (转) mysql的连接,创建账号,修改密码

    原文:http://blog.chinaunix.net/uid-20749043-id-1878306.html  mysql的连接,创建账号,修改密码 2008-10-13 15:31:29 分类 ...

  10. In p = new Fred(), does the Fred memory “leak” if the Fred constructor throws an exception?

    No. If an exception occurs during the Fred constructor of p = new Fred(), the C++ language guarantee ...