[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 ...
随机推荐
- ViewPager实现无限循环
引言 这两天在项目里实用到ViewPager来做广告运营位展示.看到如今非常多APP的广告运营位都是无限循环的,所以就研究了一下这个功能的实现. 先看看效果 从一个方向上一直滑动.么有滑到尽头的感觉. ...
- python之模块datetime 常见操作
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块datetime #http://blog.sina.com.cn/s/blog_6c37 ...
- 一网打尽2013最常用的NoSQL数据库
摘要:与关系数据库相比,每个NoSQL都有自己不同的适用场景,这里带大家盘点文档数据库.图数据库.键值数据存储.列存储数据库与内存数据网络等领域的常用的NoSQL. 在几年内,NoSQL数据库一直 ...
- JUC-线程八锁
1,一个对象里面如果有多个synchronized方法,某一个时刻内,只要一个线程去调用其中的一个synchronized方法了,其它的线程都只能等待, 换句话说,某一个时刻内,只能有唯一一个线程去访 ...
- Windows TCP/IP 临时、保留和封锁端口行为
由网络专家发布 转自:http://blog.163.com/abslh123@126/blog/static/792137962008628105919348/ 本文介绍 Windows 套接字应用 ...
- Android 自定义控件-高仿猎豹清理大师自定义内存开口圆环控件
技术:Android+java 概述 看见猎豹清理大师的内存开口圆环比例进度 挺有意思的,于是就是想自己实现下这样的效果,于是反编译了猎豹清理 大师的app看了下,原来是有两张图,于是脑子里就过了 ...
- android.telephony.SmsManager.sendMultipartTextMessage
smsManager.sendMultipartTextMessage(destAddress, null, divideContents, sentIntents, null); next step ...
- asiHttpRequst 学习地址
最全面的地址 http://blog.csdn.net/uxyheaven/article/details/7884734 http://allseeing-i.com/ASIHTTPRequest/ ...
- BigDecimal的用法详解(保留两位小数,四舍五入,数字格式化,科学计数法转数字,数字里的逗号处理)
转自:https://blog.csdn.net/ochangwen/article/details/51531866 一.简介 Java在java.math包中提供的API类BigDecimal,用 ...
- Tex
关于Tex,有许多概念.许多软件.这些软件是做什么的,要解决哪些问题,今日好好总结一下. 就像任何领域一旦引入代数系统,一切都会变得井然有序一样.就像用五行八卦描述事物,事物的规律就变得清晰一样,类比 ...