[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 ...
随机推荐
- 〖Linux〗Ubuntu设定Proxy及忽略Proxy
1. 设定代理:. ~/.proxyenv #!/bin/sh # for terminal export proxyserveraddr=123.123.123.123 export proxyse ...
- 【DB2】监控临时表空间使用
在我们使用数据库的时候,我们都知道应用程序在DB2上运行时,会产生临时表空间,我们想要监测这些临时表空间的使用情况,可以使用以下步骤: (1)打开monitor switches 中的table监视器 ...
- 【DB2】监控动态SQL语句
一.db2监控动态SQL(快照监控) db2示例用户登陆后,使用脚本语句db2 get snapshot for all on dbname>snap.out 也可以使用db2 get snap ...
- python之模块datetime详解
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块datetime详解 import datetime #data=datetime.dat ...
- 搭建Docker私有仓库--自签名方式
为了能集中管理我们创建好的镜像,方便部署服务,我们会创建私有的Docker仓库.通读了一遍官方文档,Docker为了确保安全使用TLS,需要CA认证,认证时间长的要钱啊,免费过期时间太短,还是用自签名 ...
- C#并行编程-PLINQ:声明式数据并行-转载
C#并行编程-PLINQ:声明式数据并行 目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 C#并行编程-线程同步原语 C#并行编程-P ...
- Photoshop CS6 操作记录
全局快捷键 橡皮 E 画笔 B 魔棒工具 W 钢笔工具 P 选区工具 M 移动画布 按住Space后鼠标拖动 放大缩小画布 Ctrl+-, Ctrl++ 调出/收回标尺 Ctrl+R 调整画笔大小 [ ...
- Mac 升级 PHP 7
http://www.phpyc.com/article/15 mac 自带 php, 这个地球人都知道 在新系统中,/usr/bin 成为了系统保护目录,所以我们以前使用的替换 系统 php 的方法 ...
- Linux修改终端提示符
打开~/.bashrc可以看到命令提示的内容为:\u@\h\w\$ \u表示用户名,\h表示主机名,\w表示当前目录,\$表示命令提示符(普通用户$,超级用户#) 这个命令提示符有点长,很碍事,\u@ ...
- maven groupID 和 ArtifactID的区别与作用
GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构. ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称.一般Gro ...