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. [Jquery]tab页面切换效果

    思路:取得头部和内容的div,头部当前点击的高亮,其余的去除高亮,并通过index()方法获得当前点击的索引,然后内容div通过.eq(index)显示和隐藏 优化:当前做法,从第一个快速移到最后一个 ...

  2. 启动 nginx 失败 "fastcgi_pass" directive is duplicate

    [emerg] 4953#0: "fastcgi_pass" directive is duplicate in /etc/nginx/sites-enabled/default: ...

  3. [ASP.NET] 使用Loading遮罩防止使用者重複點擊

    From: http://www.dotblogs.com.tw/joysdw12/archive/2012/12/13/85629.aspx 前言 在網頁執行中可能會因為資料量大或其他原因影響使用者 ...

  4. ACM2013.9.7

    acm还是要好好做的,还有好多的东西要学呢!不能不认真对待了,该玩够了!

  5. Tab切换栏

    // Tab切换栏 function setTab(name, m, n) { for (var i = 1; i <= n; i++) { var menu = document.getEle ...

  6. AudioQueue语音流 speex压缩 实时通讯 对讲机

    参开 http://blog.csdn.net/liulina603/article/details/19029727    博客有多篇文章 http://blog.csdn.net/liulina6 ...

  7. static声明初始化块的一下注意事项

    通过输出结果,我们可以看到,程序运行时静态初始化块最先被执行,然后执行普通初始化块,最后才执行构造方法.由于静态初始化块只在类加载时执行一次,所以当再次创建对象 hello2 时并未执行静态初始化块.

  8. windows8.1 plsql连接oracle

    http://pan.baidu.com/share/link?shareid=3782452820&uk=3557941237 http://pan.baidu.com/share/link ...

  9. 2016 - 1 -19 初学HTML5 第一天

    1.HTML COMMANDS MHTL commands called elements.Usually, an element has a start tag and an end tag e.g ...

  10. 极客DIY:打造属于自己的无线移动渗透测试箱

    本文中介绍的工具.技术带有一定的攻击性,请合理合法使用. 你想不想拥有一款属于自己的移动无线渗透测试箱,如果你感兴趣,下面介绍的设备将会对你很有帮助.这个箱子被称为“MiTM(中间人攻击)WiFi箱” ...