此题如果 #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. Maven常用指令

    Maven是通过pom.xml配置好一系列操作,然后通过命令行来进行控制操作的.格式为 mvn 命令 常用命令主要有: mvn 命令 -v:查看maven版本 compile:编译项目 test:测试 ...

  2. JAVA & Android 等待线程池内任务全部完成后退出

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

  3. Nginx配置站点适配PC和手机

    考虑到站点的在多种设备下的兼容性,有非常多站点会有手机版和电脑版两个版本号.訪问同一个站点URL,当服务端识别出用户使用电脑訪问.就打开电脑版的页面,用户假设使用手机訪问,则会得到手机版的页面. 1. ...

  4. 【Linux】ssh建立隧道tunnel连接到内网设备

    root@192.168.1.105 建立隧道: ssh -l root -N -f -R 9103:127.0.0.1:2222 work@11.11.13.17 解析:把本地127.0.0.1:2 ...

  5. 【Linux】将一个命令的输出发送给另外一个命令

    一个命令的输出可以作为下一个命令的输入,下一个命令的输出又会传递给下一个命令 我们通常使用管道和子shell的方法来组合多个命令的输出 格式 $ cmd1 |cmd2 | cmd3 这里的3个组合命令 ...

  6. 使用springMVC和Jquery实现JSONP

    JSONP这个东东是啥我就不写了,直接贴实现的代码 JAVA代码: /** * * 查询用户是否已经提交认证获取已经是认证会员 * * 使用spring mvc的直接返回string会遇到分号转义后字 ...

  7. python3用http.server模块搭建简易版服务器

    基本流程: 1.需要的支持 1)python3用http模块下的子模块,即:http.server模块 2)将希望共享的文件放在c盘下,如:C:\游戏行业面试专用 2.打开cmd,cd c:\\pyt ...

  8. mongobooster 的使用

    mongobooster是mongodb的客户端工具 1.配置数据库 file->connect..->from URL 2.数据查询 选中数据库名,右击-Open Shell->输 ...

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

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

  10. 非常漂亮滴皮肤skin++ 终极破解之法

    破解includeparametershook汇编windows *[标题]:Skin++通用界面换肤系统V2.0.1破解探讨 *[作者]:gz1X <gz1x(at)tom(dot)com&g ...