很简单一道题,搞错了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. Struts2学习笔记四:深入拦截器

    一:拦截器的工作原理 拦截器的执行过程可以类比filter过滤器,ActionInvocation实例执行过程中,先执行action实例上引用的拦截器们,然后才执行action实例处理请求,返回res ...

  2. Mysql查询优化之 触发器加中间表 方法优化count()统计大数据量总数问题

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6138288.html 在上一篇博文我们提到,分页有三种方法.其中,第三种是我们最常用的.然而,在实际应用过程中 ...

  3. J2EE开发时的包命名规则,养成良好的开发习惯

    代码编写规范目的:能够在编码过程中实现规范化,为以后的程序开发中养成良好的行为习惯.代码编写规范使用范围:J2EE项目开发.包命名规范:目的:包的命名规范应当体现出项目资源良好的划分 servlet类 ...

  4. android使用百度地图SDK获取定位信息

    本文使用Android Studio开发. 获取定位信息相对简单.我们仅仅须要例如以下几步: 第一步,注冊百度账号,在百度地图开放平台新建应用.生成API_KEY.这些就不细说了,请前往这里:titl ...

  5. 详解 KDTree

    https://blog.csdn.net/qing101hua/article/details/53228668

  6. eclipse debug模式下总是自动跳到ThreadPoolExecutor.java类

      1.情景展示 使用eclipse,debug模式运行项目时,总是会调用这个ThreadPoolExecutor.java类,明明没有打断点却自动停止运行. 2.原因分析 在eclipse中,默认是 ...

  7. Knockout学习之表单绑定器(下)

    “hasFocus”绑定 hasFocus绑定器会将DOM元素的焦点状态与视图模型中的属性相关联,当你设置视图模型中关联的属性为true或false后,将能够设置关键的DOM元素是否获得焦点. 比如下 ...

  8. V-rep学习笔记:Reflexxes Motion Library 2

    VREP中的simRMLMoveToPosition函数可以将静态物体按照设定的运动规律移动到指定的目标位置/姿态.If your object is dynamically enabled, it ...

  9. AJAX的同步返回结果值

    function makeJQGridDataFromList(url) {     var rowData;     var viewPage = 0;     var viewTotal = 0; ...

  10. 代理服务 SQUID 测试

    第一部分:SQUID基础 Squid代理服务的基本配置: http_port 3128                    #设置监听的IP与端口号 cache_mem 64 MB          ...