此题如果 #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. JAVA & Android 等待线程池内任务全部完成后退出

    void shutdownAndAwaitTermination(ExecutorService pool) { pool.shutdown(); // Disable new tasks from ...

  2. 【转发】PHP连接MSSQL数据库案例,PHPWAMP多个PHP版本连接SQL Server数据库

    转发自:http://blog.csdn.net/lccee/article/details/54289076 课前小知识普及:MSSQL和SQL Server是同一个软件,叫法不同而已,MSSQL全 ...

  3. python3新特点

    #xiaodeng #python 3 #1.编码方式和性能 ''' 1.py3运行效率更快 2.默认源文件编码ASCII变为UTF-8,以前文件前加入的coding=utf-8不再需要 3.针对un ...

  4. ArcGIS调整影像颜色输出

    有碰到一些质量很差的遥感影像,颜色需要进行调整(主要是针对看)输出,这里记录一下ArcGIS中的调整输出方式. 1.首先把影像文件拖入ArcMap中,然后右键单击图层列表中的图像,选择属性. 2.选择 ...

  5. VSTS 免费代码git/tfs托管体验-使用代码云托管

    虽然各种代码托管平台很多.真正免费的私有仓储 却很少.微软的东西还是值得一用.免费版,5个用户.够了. 申请地址: https://www.visualstudio.com/zh-hans/free- ...

  6. javascript基础 思维导图2

    来源于:http://www.cnblogs.com/xianyulaodi/p/5886128.html 1.javascript数据类型 2.javascript数组 3.javascript运算 ...

  7. iOS 适用于Pad上的菜单弹出界面-最简单的一种实现记录

    前言: 此种方式实现只适用于pad开发,在iPhone上是无效的. 实现: 比如我在界面上有一个按钮,点击按钮,在按钮旁边弹出一个Pop框. 1.按钮点击事件 btn.addTarget(self, ...

  8. jeecg平台testDatagrid

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. sql server @@ROWCOUNT 会被 if 给 清 0

    官方说 @@ROWCOUNT  会被以下几种语句清0 原文如下: Statements such as USE, SET <option>, DEALLOCATE CURSOR, CLOS ...

  10. cobbler启动问题

    [root@localhost ~]# cobblerTraceback (most recent call last): File "/usr/bin/cobbler", lin ...