[Leet Code]Path Sum II
此题如果 #1 和 #4 判断分支交换,大集合就会超时(因为每次对于非叶子节点都要判断是不是叶子节点)。可见,有时候if else判断语句也会对于运行时间有较大的影响。
import java.util.ArrayList;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
private int currSum = 0;
private ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
private ArrayList<Integer> tryPath = new ArrayList<Integer>();
private ArrayList<Integer> oneSuccPath;
public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
result.clear();
tryPath.clear();
if (null == root)
return result;
pathSumCore(root, sum);
return result;
}
public void pathSumCore(TreeNode root, int sum) {
// Start typing your Java solution below
// DO NOT write main() function
if (null == root)
return;
currSum += root.val;
tryPath.add(root.val);
// #1 左右孩子都有
if (null != root.left && null != root.right) {
pathSumCore(root.left, sum);
pathSumCore(root.right, sum);
currSum -= root.val;
tryPath.remove(tryPath.size()-1);
return;
}
// #2 只有右孩子
else if (null == root.left && null != root.right) {
pathSumCore(root.right, sum);
currSum -= root.val;
tryPath.remove(tryPath.size()-1);
return;
}
// #3 只有左孩子
else if (null == root.right && null != root.left) {
pathSumCore(root.left, sum);
currSum -= root.val;
tryPath.remove(tryPath.size()-1);
return;
}
// #4 叶子节点
else {//只有叶子节点才判断,其他情况都要继续往深去判断
if (currSum == sum) {
oneSuccPath = new ArrayList<Integer>(tryPath);
result.add(oneSuccPath);
currSum -= root.val;
tryPath.remove(tryPath.size()-1);
return;
}
else {
currSum -= root.val;
tryPath.remove(tryPath.size()-1);
return;
}
}
}
public static void main(String[] args) {
TreeNode a = new TreeNode(1);
TreeNode b = new TreeNode(-2);
TreeNode c = new TreeNode(-3);
TreeNode d = new TreeNode(1);
TreeNode e = new TreeNode(3);
TreeNode f = new TreeNode(-2);
TreeNode g = new TreeNode(-1);
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.left = f;
d.left = g;
Solution sl = new Solution();
sl.pathSum(a, 2);
}
}
[Leet Code]Path Sum II的更多相关文章
- [Leet Code]Path Sum
很简单一道题,搞错了N次,记录一下. public class Solution { private int currSum = 0; public boolean hasPathSum(TreeNo ...
- [原创]leet code - path sum
; ; ; } } ; }};
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【leetcode】Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 32. Path Sum && Path Sum II
Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II
Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...
随机推荐
- Linux下通用打印系统CUPS使用教程
昨天研究了一下关于在Linux下实现打印操作的相关内容,整理记录如下: 1.什么是CUPS CUPS(Common UNIX Printing System,即通用Unix打印系统)是FedoraCo ...
- Orabbix监控Oracle 11g
Orabbix简介说明 orabbix是一个用来监控oracle数据库性能的zabbix插件工具,通过安装在被监控服务器上客户端上收集数据并传给zabbix服务器端,然后通过调用图形显示.具有以下功能 ...
- 将excel表导入到mysql中
//导入excel表 方法一: )打开Excel另存为CSV文件 )将文件编码转化为utf8,用NotePad++打开csv文件,选择格式—转为utf8编码格式—保存 )在MySQL建表,字段的顺序要 ...
- Linux(centos6.5)下安装jenkins
Jenkins 的前身是 Hudson 是一个可扩展的持续集成引擎. 通俗的来讲,jenkins就是一个可以实现自动化部署的一个插件, 对于我来说,也是应用在系统部署上. 废话不多说,直接进入我们的安 ...
- Inno Setup入门(三)——指定压缩方式
Setup段中的compression指定了采用的压缩方式,较高的压缩率需要较多的时间或者需要更大的内存空间,可用的值如下: zip zip/1到zip/9 bzip bzip/1 到bzip/9 l ...
- Gerrit最简工作流
Gerrit最简工作流: git clone ... //注意clone with commit-msg hook,否则push可能失败(ERROR: missing Change-Id in c ...
- ios中NSObject分类(2)
#import <Foundation/Foundation.h> UIColor * rgb(int r, int g, int b); UIColor * rgbA(int r, in ...
- asyncsocket的用法
更多参考文章 http://blog.csdn.net/zltianhen/article/details/6560322 http://www.cnblogs.com/bucengyongyou/a ...
- IOS中文本框输入自动隐藏和自动显示
uilabe和UIText扩展方法 +(UILabel*)LabWithFrame:(CGRect)_rect text:(NSString*)aText textColor:(UIColor*)aC ...
- 实测iOS Dynamic Framework 对 App 启动时间的影响效果
最近看到的Slow App Startup Times里提到: The dynamic loader finds and reads the dependent dynamic libraries ( ...