此题如果 #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的更多相关文章

  1. [Leet Code]Path Sum

    很简单一道题,搞错了N次,记录一下. public class Solution { private int currSum = 0; public boolean hasPathSum(TreeNo ...

  2. [原创]leet code - path sum

    ;            ;                ;                            }        }        ;            }};

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. 【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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Oracle 中包(Package)

    一.什么要使用包?        在一个大型项目中,可能有很多模块,而每个模块又有自己的过程.函数等.而这些过程.函数默认是放在一起的(如在PL/SQL中,过程默认都是放在一起 的,即Procedur ...

  2. Generating phar.phar chmod: cannot access `ext/phar/phar.phar': No such file or directory make: [ext/phar/phar.phar] Error 1 (ignored)

    make install出现了cp: cannot stat `ext/phar/phar.phar': No such file or directory 于是我又: cd ext/phar/ls ...

  3. Libevent官方代码样例学习(二)

    连接监听器: 接收TCP连接请求 evconnlistener机制用于监听并接受TCP连接请求. 这些方法在event2/listener.h中声明, 在Libevent 2.0.2-alpha之后的 ...

  4. TCP/IP协议——TCP/IP协议栈及框架

    TCP/IP协议同ISO/OSI模型一样,也可以安排成栈形式.但这个栈不同于ISO/OSI版本,比ISO/OSI栈少,所以又称之为短栈.另外,需要知道的是:TCP/IP协议栈只是许多支持ISO/OSI ...

  5. nginx搭建httpsserver

    HTTPS简单介绍 HTTPS(Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单来讲就是HTTP的安全版. ...

  6. ios core plot设置xy坐标

    #import "ViewController.h" @interface ViewController () //要绘制基于x,y轴的图形 @property(nonatomic ...

  7. 在 iOS 中实现方法链调用

    编译:伯乐在线 - 林欣达 如有好文章投稿,请点击 → 这里了解详情 如需转载,发送「转载」二字查看说明 前言 链式调用(chained calls)是指在函数调用返回了一个对象的时候,使得这个调用链 ...

  8. 基于 CADisplayLink 的 FPS 指示器详解

    前言 之前在开发中有使用到计时器NSTimer,后来了解到iOS中不同的计时方法,其中就包括了CADisplayLink.基于CADisplayLink以屏幕刷新频率同步绘图的特性,尝试根据这点去实现 ...

  9. C# WinForm给Button或其它控件添加快捷键响应

    今天做东西遇到要给按钮添加快捷键.就在这介绍三种添加快捷键的方式. 第一种Alt + *(按钮快捷键) 在大家给button.label.menuStrip等控件设置Text属性时在名字后边加& ...

  10. python对文件操作

    python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...