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


linkedin 只找一个但是只需要输出一条path,并不是让你简单说说思路就开始写代码,而是只跟你说一个大概让你自己开始讨论。 

包括从treenode的定义(有没有父节点,value是int还是double),到用bfs和dfs的选择,到tree里面正负值的影响,到输出是用List还是用其他数据结构的利弊等等。
The basic idea is to subtract the value of current node from sum until it reaches a leaf node and the subtraction equals 0,
then we know that we got a hit. Otherwise the subtraction at the end could not be 0.

 

class TreeNode {
     int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
}

public class BinaryTree {
    public List<Integer> pathSum(TreeNode root, int sum) {
        List<Integer> list = new ArrayList<Integer>();
        if (root == null) {

            return list;
        }
        sum -= root.val;

        if (root.left == null && root.right == null) {
            if (sum == 0) {
                list.add(root.val);

            }
            return list;
        }

        List<Integer> left = pathSum(root.left, sum);
        if (!left.isEmpty()) {
            list.add(root.val);
            list.addAll(left);
            return list;
        }
        List<Integer> right = pathSum(root.right, sum);
        if (!right.isEmpty()) {
            list.add(root.val);
            list.addAll(right);
            return list;
        }
        return list;
    }
    public static void main(String[] args) {
        BinaryTree tes = new BinaryTree();
        TreeNode root = new TreeNode(5);
        TreeNode root1 = new TreeNode(4);
        TreeNode root2 = new TreeNode(11);
        TreeNode root3 = new TreeNode(7);
        TreeNode root4 = new TreeNode(2);
        TreeNode root5 = new TreeNode(8);
        TreeNode root6 = new TreeNode(13);
        TreeNode root7 = new TreeNode(4);
        TreeNode root8 = new TreeNode(5);
        root.left = root1;
        root1.left = root2;
        root2.left = root3;
        root2.right = root4;
        root.right = root5;
        root5.left = root6;
        root5.right = root7;
        root7.left = root8;
        List<Integer> ans = new ArrayList<>();
        ans = tes.pathSum(root, 22);
        for (int i : ans) {
            System.out.println(i);
        }
    }

}

  



判断左右子树是否为空避免多次递归左右空子树, if (root.left == null && root.right == null)
本层加, 本层删除,遍历兄弟节点:
list.add(root.val);
        //dfs中二叉树的遍历
        helper(root.left, sum, res, list);
        helper(root.right, sum, res, list);
        list.remove(list.size() - 1);

/**
 * 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>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<Integer>();
        helper(root, sum, res, list);
        return res;
    }
    private void helper(TreeNode root, int sum, List<List<Integer>> res,
                        List<Integer> list) {
        if (root == null) {
            
            return;
        } 
        sum -= root.val;
        if (root.left == null && root.right == null) {
            if (sum == 0) {
                list.add(root.val);
                res.add(new ArrayList<Integer>(list));
                list.remove(list.size() - 1); 
            }
            return;
        }
        list.add(root.val);
        //dfs中二叉树的遍历
        helper(root.left, sum, res, list);
        helper(root.right, sum, res, list);
        list.remove(list.size() - 1);
    }
}

递归出口1, 出口2(操作, 遍历(dfs), 操作), 操作, 遍历(dfs), 操作

posted @ 2017-07-25 18:36  apanda009  阅读(123)  评论(0编辑  收藏  举报