[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〗Kubuntu14.04 平滑字体的设置
有没有感觉终端的字体锯齿感觉非常强? 经过搜索后发现可以平滑字体显示得更漂亮一点: System Settings > Application Appearance > Fonts I e ...
- 4、线程范围内的数据共享之ThreadLocal
/** * 线程范围类的数据共享 * 核心:ThreadLocal类 * 实际场景: * Hibernate的getCurrentSession方法,就是从线程范围内获取存在的session,如果不存 ...
- HP LoadRunner:怎样连接到HP ALM的CAC模式?
你听说过使用HP LoadRunner连接到HP ALM的CAC模式么?本文给大家讲讲什么是CAC,怎样连接到HP ALM的CAC模式中. CAC是什么? CAC全称是Common Access Ca ...
- vmware安装找不到虚拟网卡解决方案
前一段实际,win7升级到win10发现vmware12没有虚拟网卡vnet1/vnet8.这不坑爹吗,没网卡能通信吗? 在网上搜寻一下发现可以重置网络,即可再次安装虚拟网卡...算是对问题的记录学习 ...
- 使用maven-assembly-plugin打包zipproject
使用Maven对Web项目进行打包.默觉得war包.但有些时候.总是希望打成zip包(亦或其它压缩包,类似tomcat的那种文件夹结构,直接运行bin/startup.sh就能够),maven-war ...
- 使用Thrift让Python为Java提供服务
Thrift是基于TCP的,谷歌的GRPC是基于HTTP的.Thrift和GRPC都是比直接写个web接口进行调用更完美的方式,最明显的一点就是:我们可以定义结构体,避免了手动解析的过程. 但是,在将 ...
- 树莓派进阶之路 (019) - 树莓派通过filezilla,samba与PC文件共享(转)
虽然我们可以很方便的通过ssh譬如putty或者vnc连接操控树莓派,但是毕竟树莓派资源没那么高,在上面编程,调试要吃力的多.所以还是想在pc上编程上传到树莓派或者最好,文件共享,可以直接读写共同的文 ...
- 信号量 Linux函数 semget();semctl();semop();(转)
本文出自:http://blog.csdn.net/ta893115871/article/details/7505560 Linux进程通信之信号量 信号量(semaphore)是变量,是一种特殊的 ...
- OAuth的机制原理讲解及开发流程(转)
1.OAuth的简述 OAuth(Open Authorization,开放授权)是为用户资源的授权定义了一个安全.开放及简单的标准,第三方无需知道用户的账号及密码,就可获取到用户的授权信息,并且这是 ...
- 【MyBatis】MyBatis之别名typeAliases标签的使用
<configuration> <typeAliases> <typeAlias alias="Dept" type="cn.xdl.ent ...