Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

 

分治法可以解决,传入下层的sum为原来的sum-root.val即可

1 public boolean hasPathSum(TreeNode root, int sum) {
2         if(root==null)
3             return false;
4         if(root.left==null && root.right==null)
5             return root.val==sum;
6         return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val);
7 }

 

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]
]

 

II就是要返回所有可能的path. 可以用分治法的思想去实现(把根节点加到左子树得到的list和右子树得到的list的第一位),不过较慢,因为要结果返回给上层。用单纯的dfs回溯也能很好地实现,而且较快。

分治法:

 1  public List<List<Integer>> pathSum(TreeNode root, int sum) {
 2         List<List<Integer>> re = new ArrayList<List<Integer>>();
 3         if(root==null)
 4             return re;
 5         if(root.left==null && root.right==null && root.val==sum) {
 6             List<Integer> temp = new ArrayList<Integer>();
 7             temp.add(root.val);
 8             re.add(temp);
 9             return re;
10         }
11         List<List<Integer>> left = pathSum(root.left, sum-root.val);
12         List<List<Integer>> right = pathSum(root.right, sum-root.val);
13         if(left.size()>0)
14             for(int i=0;i<left.size();i++) {
15                 left.get(i).add(0,root.val);
16                 re.add(left.get(i));
17             }
18         if(right.size()>0)
19             for(int i=0;i<right.size();i++) {
20                 right.get(i).add(0,root.val);
21                 re.add(right.get(i));
22             }
23         return re;
24     }

dfs:

public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> re = new ArrayList<List<Integer>>();
        if(root==null)
            return re;
        List<Integer> path = new ArrayList<Integer>();
        collect(re, path, root, sum);
        return re;
    }
    
    public void collect(List<List<Integer>> re, List<Integer> path, TreeNode rt, int v) {
        if(rt.left==null && rt.right==null && rt.val==v) {
            List<Integer> temp = new ArrayList<Integer>(path);
            temp.add(rt.val);
            re.add(temp);
            return;
        }
        path.add(rt.val);
        if(rt.left!=null)
            collect(re,path,rt.left,v-rt.val);
        if(rt.right!=null)
            collect(re, path, rt.right, v-rt.val);
        path.remove(path.size()-1);
    }

 

posted on 2015-04-08 03:30  metalx  阅读(154)  评论(0编辑  收藏  举报