leetcode:Path Sum II
2014-06-11 22:01
225 查看
Path Sum I 的变形,只是在PathSun1的情况要求出所有可能的情况
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { static int ans = 0; static ArrayList<Integer> stack = new ArrayList<Integer>(); static ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) { ans = 0; stack.clear(); list.clear(); traverl(root, sum); return list; } public static boolean isLeaf(TreeNode root){ return root.left == null && root.right == null; } static void traverl(TreeNode root, int sum){ if(root != null){ stack.add(root.val); ans += root.val; if(isLeaf(root) && ans == sum){ ArrayList<Integer> t = new ArrayList<Integer>(); for(int i = 0; i < stack.size();++i){ t.add(stack.get(i)); } list.add(t); } traverl(root.left, sum); traverl(root.right, sum); stack.remove(stack.size() - 1); ans -= root.val; } } }
相关文章推荐
- 【LeetCode】Path Sum II
- leetcode_middle_88_113. Path Sum II
- LeetCode题解:Path Sum II
- leetcode: Path Sum II
- LeetCode | Path Sum II
- LeetCode:113 Path Sum II
- [LeetCode] Path Sum II
- [LeetCode]Path Sum II
- LeetCode[Tree]: Path Sum II
- Leetcode--Path Sum II
- [LeetCode]题解(python):113-Path Sum II
- LeetCode(113) Path Sum II
- LeetCode Path Sum II
- LeetCode_Path Sum II
- [Leetcode] Path Sum II路径和
- LeetCode113 Path Sum II
- 【LeetCode】Path Sum II
- LeetCode 113. Path Sum II
- LeetCode Path Sum II
- leetcode113~Path Sum II