很简单一道题,搞错了N次,记录一下。

public class Solution {
private int currSum = 0; public boolean hasPathSum(TreeNode root, int sum) {
if (null == root)
return false; currSum = 0;
return hasPathSumCore(root, sum);
} public boolean hasPathSumCore(TreeNode root, int sum) {
// Start typing your Java solution below
// DO NOT write main() function
if (null == root)
return true; currSum += root.val; boolean leftHas = false;
boolean rightHas = false; if (null == root.left && null == root.right) {
if (currSum == sum) {
currSum -= root.val;
return true;
}
else {
currSum -= root.val;
return false;
}
}
else if (null == root.left && null != root.right) {
rightHas = hasPathSumCore(root.right, sum);
currSum -= root.val;
return rightHas;
}
else if (null == root.right && null != root.left) {
leftHas = hasPathSumCore(root.left, sum);
currSum -= root.val;
return leftHas;
}
else {
leftHas = hasPathSumCore(root.left, sum);
rightHas = hasPathSumCore(root.right, sum);
currSum -= root.val;
return leftHas || rightHas;
}
}
}

[Leet Code]Path Sum的更多相关文章

  1. [Leet Code]Path Sum II

    此题如果 #1 和 #4 判断分支交换,大集合就会超时(因为每次对于非叶子节点都要判断是不是叶子节点).可见,有时候if else判断语句也会对于运行时间有较大的影响. import java.uti ...

  2. [原创]leet code - path sum

    ;            ;                ;                            }        }        ;            }};

  3. 【LeetCode OJ】Path Sum II

    Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...

  4. 【LeetCode OJ】Path Sum

    Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...

  5. LintCode Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  6. Project Euler 83:Path sum: four ways 路径和:4个方向

    Path sum: four ways NOTE: This problem is a significantly more challenging version of Problem 81. In ...

  7. Project Euler 82:Path sum: three ways 路径和:3个方向

    Path sum: three ways NOTE: This problem is a more challenging version of Problem 81. The minimal pat ...

  8. [LeetCode] 437. Path Sum III_ Easy tag: DFS

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. Leet Code 771.宝石与石头

    Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S  ...

随机推荐

  1. Maven依赖传递、依赖传递排除、依赖冲突

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6628429.html  一:Maven依赖传递 假如有Maven项目A,项目B依赖A,项目C依赖B.那么我们可 ...

  2. 6、java5线程池之固定大小线程池newFixedThreadPool

    JDK文档说明: 创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程.在任意点,在大多数 nThreads 线程会处于处理任务的活动状态.如果在所有线程处于活动状态时提交附加任务,则 ...

  3. 推荐一款VS2008代码增强插件——MetalScroll

    时光如水,岁月如歌.虽然现在已经是2013年底马上就要步入2014了,但还是有很多人在使用VS2008开发项目,今天要推荐一款VS2008(同时支持VS2005,但不支持VS2010)代码增强插件给仍 ...

  4. 【mysql】数据库Schema的优化

    由于MySQL数据库是基于行(Row)存储的数据库,而数据库操作 IO 的时候是以 page(block)的方式,也就是说,如果我们每条记录所占用的空间量减小,就会使每个page中可存放的数据行数增大 ...

  5. ios中Pldatabase的用法(3)

    #import "ViewController.h" @interface ViewController () @property(nonatomic,retain)PLSqlit ...

  6. kickstart命令选项

    下面的选项可以放入kickstart文件.如果喜欢使用图形化的界面来创建kickstart文件,可以使用"Kickstart配置"应用程序.(注:如果某选项后面跟随了一个等号(=) ...

  7. java结合使用Jsonp的例子

    更多:js跨域问题解释 解决方案值使用jsonp或jQuery Jsonp和java操作例子 介绍JSONP之前,先简单的介绍一些JSON.JSON是JavaScript Object Notatio ...

  8. UNIX 家族及Linux

    Unix成长为一个非私有的操作系统,是因为1956年的AT&T公司受命于联邦去经营电报电话服务.当然也可以开发软件,甚至那个软件可以有”合理”收费的许可证,但是这个公司却被禁止从事任何和计算机 ...

  9. Intel Edison学习笔记(一)—— 刷系统

    一.下载安装包 1.固件安装包:官网下载地址:http://downloadmirror.intel.com/ ... image-ww25.5-15.zip2 2.烧录工具下载地址:http://d ...

  10. 16条Android开发小经验

    1. TextView中的getTextSize返回值是以像素(px)为单位的, 而setTextSize()是以sp为单位的. 所以如果直接用返回的值来设置会出错,解决办法是 用setTextSiz ...