113. Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1

return

[
   [5,4,11,2],
   [5,8,4,5]
]

 

 

Subscribe to see which companies asked this question

Hide Tags
 Tree Depth-first Search
Hide Similar Problems
 (E) Path Sum (E) Binary Tree Paths
 
 
Backtracking solution:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum){
        List<List<Integer>> result  = new LinkedList<List<Integer>>();
        List<Integer> currentResult  = new LinkedList<Integer>();
        pathSum(root,sum,currentResult,result);
        return result;
    }

    public void pathSum(TreeNode root, int sum, List<Integer> currentResult, List<List<Integer>> result) {
        if (root == null)
            return;
        currentResult.add(new Integer(root.val));
        if (root.left == null && root.right == null && sum == root.val) {
            result.add(new LinkedList(currentResult));
        } else {
            pathSum(root.left, sum - root.val, currentResult, result);
            pathSum(root.right, sum - root.val, currentResult, result);
        }
        currentResult.remove(currentResult.size() - 1);  //back-tracking.
    }
}

 

 
Easy to think recursive solution:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        ArrayList<List<Integer>> ret = new ArrayList<List<Integer>>();
        
        if(root == null)
            return ret;
        
        if(root.left == null && root.right == null)
        {
            if(root.val == sum)
            {
                List<Integer> r = new LinkedList<Integer>();
                r.add(sum);
                ret.add(r);
                return ret;
            }
            else
            {
                return ret;
            }
        }
        
        if(root.right != null)
        {
            List<List<Integer>> list = pathSum(root.right, sum - root.val);
            if(list.size() != 0)
            {
                for(List<Integer> childrenPath : list)
                {
                    List<Integer> n = new LinkedList<Integer>();
                    n.add(root.val);
                    n.addAll(childrenPath);
                    ret.add(n);
                }
            }
        }
        
        if(root.left != null)
        {
            List<List<Integer>> list = pathSum(root.left, sum - root.val);
            if(list.size() != 0)
            {
                for(List<Integer> childrenPath : list)
                {
                    List<Integer> n = new LinkedList<Integer>();
                    n.add(root.val);
                    n.addAll(childrenPath);
                    ret.add(n);
                }
            }
        }
        
        return ret;
    }
}

 

 
 
 
posted @ 2016-05-06 13:02  新一代的天皇巨星  阅读(161)  评论(0)    收藏  举报