此题如果 #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. 转发:centos彻底删除文件夹、文件命令(centos 新建、删除、移动、复制等命令)

    http://blog.csdn.net/lpdx111/article/details/16877725 centos彻底删除文件夹.文件命令(centos 新建.删除.移动.复制等命令: 1.新建 ...

  2. LeetCode] binaryTreePaths

    class Solution { public: void binaryPath(TreeNode* root, vector<string>& result, string pa ...

  3. python之模块copy,了解概念即可

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块copy,了解概念即可 import copy #浅拷贝 #copy拷贝一个对象,但是对象 ...

  4. WinHttp编写HTTP服务器示例代码

    这是微软提供的示例程序,原文地址在此https://msdn.microsoft.com/en-us/library/windows/desktop/aa364640(v=vs.85).aspx HT ...

  5. vijos 1006 晴天小猪历险记之Hill——数字三角形的终极变化

    题目链接:https://vijos.org/p/1006 数字三角形原题看这里:http://www.cnblogs.com/huashanqingzhu/p/7326837.html 背景 在很久 ...

  6. Jenkins执行批处理文件失败

    今天搭建Jenkins持续集成环境,编译环境是.net,在.net下没有比较好的代码覆盖率测试插件,所以用了开源的OpenCover,计算代码覆盖率,然后用ReportGenerator导出hmtl格 ...

  7. windows DLL中使用COM的注意事项

    windows的DLL开发是有需要注意的地方的,不然会造成一些问题.由于我最近的使用不当,又造成了问题,虽然之前有写过一篇笔记, http://www.cnblogs.com/foohack/p/66 ...

  8. ios中自定义checkbox

    //自定义button#import <UIKit/UIKit.h> @interface CKButton : UIButton @end #import "CKButton. ...

  9. <转>LUA语法分析

    本文选自:http://www.cnblogs.com/nazhizq/p/6516561.html 一步步调试,在lparser.c文件中luaY_parser函数是语法分析的重点函数,词法分析也是 ...

  10. Quartz中时间表达式的设置-----corn表达式 (转)

    Quartz中时间表达式的设置-----corn表达式 (注:这是让我看比较明白的一个博文,但是抱歉,没有找到原作者,如有侵犯,请告知) 时间格式: <!-- s m h d m w(?) y( ...