美文网首页
【leetcode】No.113:path-sum-Ⅱ

【leetcode】No.113:path-sum-Ⅱ

作者: I讨厌鬼I | 来源:发表于2019-07-27 19:32 被阅读0次

    题目描述

    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 andsum = 22,

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

    return

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

    思路

    在遍历二叉树的时候就将节点的值放入列表中,并计算权值之和,到叶子节点与sum比较,如果相等,就把列表放入结果列表中。递归非递归都可以,注意非递归需要用后序非递归改写,只有右子树为空或者已经遍历过才能把右子节点从列表中拿出来。

    代码

    递归实现
    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    import java.util.ArrayList;
    
    public class Solution {
        ArrayList<ArrayList<Integer>> res;
        ArrayList<Integer> tmp;
    
        public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
            res = new ArrayList();
            tmp = new ArrayList();
            preOrder(root, sum);
            return res;
        }
    
        private void preOrder(TreeNode root, int sum){
            if (root != null){
                sum -= root.val;
                tmp.add(root.val);
                //和为sum时把列表放入结果列表中
                if (root.left == null && root.right == null && sum == 0){
                    res.add(new ArrayList<>(tmp));
                }
                else {
                    preOrder(root.left, sum);
                    preOrder(root.right, sum);
                }
                //遍历结束从列表中把该节点值拿出来
                tmp.remove(tmp.size() - 1);
            }
        }
    }
    
    非递归实现
    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    import java.util.ArrayList;
    import java.util.Stack;
    
    public class Solution {
        public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
            ArrayList<ArrayList<Integer>> res = new ArrayList<>();
            ArrayList<Integer> tmp = new ArrayList<>();
            Stack<TreeNode> stack = new Stack();
            TreeNode last = null;  //用于存储右节点是否被遍历过
            TreeNode cur = root;
            int count = 0;
            while (!stack.isEmpty() || cur != null){
                if (cur != null){
                    count += cur.val;
                    tmp.add(cur.val);
                    //和为sum时把列表放入结果列表中
                    if (count == sum && cur.left == null && cur.right == null){
                        res.add(new ArrayList(tmp));
                    }
                    stack.push(cur);
                    cur = cur.left;
                }
                else {
                    cur = stack.peek();
                    //如果没有遍历过则往右子树遍历
                    if (cur.right != null && last != cur.right){
                        cur = cur.right;
                    }
                    else {
                        cur = stack.pop();
                        //遍历过则从列表中拿出节点值,并把计算的总值减去
                        tmp.remove(tmp.size() - 1);
                        count -= cur.val;
                        //标记上一个节点
                        last = cur;
                        cur = null;
                    }
                }
            }
            return res;
        }
    }
    

    相关文章

      网友评论

          本文标题:【leetcode】No.113:path-sum-Ⅱ

          本文链接:https://www.haomeiwen.com/subject/xgzerctx.html