此题如果 #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. openerp 7.0邮件多用户发送失败问题 解决方法

    方法一(推荐): 修改代码/usr/lib/pymodules/python2.7/openerp/addons/base/ir/ir_mail_server.py #425 line: #mail_ ...

  2. 〖Android〗sshd for android, 及映射根文件系统至本地盘符

    严重问题: 若移植失败将可能直接导致手机***无法开机***,导入相关文件需慎重! 达成效果: 1. ssh 远程登录 Android 终端: 2. sftp 挂载/映射 Android 根文件系统至 ...

  3. Axure 图片轮播(广告通栏图片自动播放效果)

    baiduYunpan:http://pan.baidu.com/s/1eRPCy90 里面的“图片轮播”部件即可实现这个功能

  4. python3 发送邮件功能

    阿-_-涵的博客 #首先写一个模块功能,发邮件功能打包起来 from smtplib import SMTP from email.mime.text import MIMEText def send ...

  5. PHP 反射API说明

    2.API概览:class Reflection { }interface Reflector { }class ReflectionException extends Exception { }cl ...

  6. Ubuntu18.04使用f3probe检测U盘实际容量

    项目主页 https://fight-flash-fraud.readthedocs.io/ 使用f3probe 能快速检测出被测U盘的实际容量, 命令 $ sudo f3probe --destru ...

  7. C#代码优化—字符串拼接效率比较

    字符串拼接主要有以下几种方法: + : 加号 String.Format() : 字符串格式化 StringBuilder.Append() 说明 对于少量固定的字符串拼接,如string str = ...

  8. 【Mysql】php执行脚本进行mysql数据库 备份和还原

    一.mysql备份 1.这里使用 php脚本的形式进行mysql 数据库的备份和还原,想看linux的sh版本的,有时间再贴. 2.找到 mysql的[mysqldump] 执行程序,建议phpinf ...

  9. 【mysql】一条慢查询sql的的分析

    这个是我在jobbole.com 上看到的 先给出数据表table结构 mysql> show create table tt \G *************************** 1. ...

  10. node下使用jquery

    node使用jquery的两种方式 在node下,使用jquery有两种方法: 使用jsdom模拟一个window对象 使用cheerio,cheerio只实现了jquery的dom部分功能,相当于j ...