[Leet Code]Path Sum
很简单一道题,搞错了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的更多相关文章
- [Leet Code]Path Sum II
此题如果 #1 和 #4 判断分支交换,大集合就会超时(因为每次对于非叶子节点都要判断是不是叶子节点).可见,有时候if else判断语句也会对于运行时间有较大的影响. import java.uti ...
- [原创]leet code - path sum
; ; ; } } ; }};
- 【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 ...
- 【LeetCode OJ】Path Sum
Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...
- 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. ...
- 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 ...
- 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 ...
- [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 ...
- Leet Code 771.宝石与石头
Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S ...
随机推荐
- Java多线程之线程阻塞原语LockSupport的使用
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6558597.html 看名字就知道了,LockSupport——提供对加锁机制的支持. 它是提供线程阻塞的原 ...
- 〖Linux〗使用sed命令修改小端(little endian)存储的数据
#!/bin/bash - #=============================================================================== # # F ...
- 〖Linux〗Ubuntu中使用KVM安装虚拟机
1. 安装软件: sudo apt-get install libvirt0 libvirt-bin libvirt-dev virt-manager qemu-system 2. 配置网桥: # i ...
- wordpress 开源博客系统部署
1.开发工具 server apache 下载地址:http://www.apache.org http://httpd.apache.org/download.cgi 数据库 mys ...
- 安装xenapp后,非管理员连接RDP出现桌面当前不可用的解决方法
安装完xenapp后,非管理员帐号就不能远程登录到2008服务器. 修改方法如下:1.启动 Citrix AppCenter展开citrix资源.Xenapp.<场地>.策略,右面的窗格切 ...
- 理解 LDA 主题模型
前言 gamma函数 0 整体把握LDA 1 gamma函数 beta分布 1 beta分布 2 Beta-Binomial 共轭 3 共轭先验分布 4 从beta分布推广到Dirichlet 分布 ...
- 适配新路由3(D2)的LEDE/OpenWrt固件
使用MediaTek系列的芯片方案 Y1(R6830): MT7620A + MT7612EN(5G 866M) + winbond 25Q128FVFG + winbond W971GG6KB-25 ...
- df看到的文件系统容量跟parted看到的分区容量差别较大的解决方法
下午同事在自己的开发机上遇到题目说到的问题,它看到挂在到/dev/sda磁盘分区5上的ext4文件系统的容量显著小于该分区的大小 df看到的文件系统容量: #df -h /dev/sda5 Files ...
- linux shell 脚本攻略学习10--生成任意大小的文件和文本文件的交集与差集详解
一.生成任意大小的文件(dd命令): 举例: amosli@amosli-pc:~/learn/example$ ; + records in + records out bytes ( MB/s a ...
- Linux命令之lsb_release - 查看当前系统的发行版信息
用途说明 lsb_release命令用来查看当前系统的发行版信息(prints certain LSB (Linux Standard Base) and Distribution informati ...