LeetCode 145: Binary Tree Postorder Traversal

/**
 * 145. Binary Tree Postorder Traversal
 * 1. Time:O(n)  Space:O(n)
 * 2. Time:O(n)  Space:O(n)
 * 3. Time:O(n)  Space:O(n)
 * 4. Time:O(n)  Space:O(1)
 */

// 1. Time:O(n)  Space:O(n)
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        postorderHelper(root,res);
        return res;
    }
    
    public void postorderHelper(TreeNode root, List<Integer> res){
        if(root!=null){
            if(root.left!=null)
                postorderHelper(root.left,res);
            if(root.right!=null)
                postorderHelper(root.right,res);
            res.add(root.val);
        }
    }
}

// 2. Time:O(n)  Space:O(n)
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        LinkedList<Integer> res = new LinkedList<>();
        if(root==null) return res;
        Stack<TreeNode> s = new Stack<>();
        s.push(root);
        while(!s.empty()){
            TreeNode cur = s.pop();
            res.addFirst(cur.val);
            if(cur.left!=null)
                s.push(cur.left);
            if(cur.right!=null)
                s.push(cur.right);
        }
        return res;
    }
}

// 3. Time:O(n)  Space:O(n)
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        Stack<TreeNode> s = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        do{
            while(cur!=null){
                s.push(cur);
                cur = cur.left;
            }
            prev = null;
            while(!s.empty()){
                cur = s.pop();
                if(cur.right==prev){
                    res.add(cur.val);
                    prev = cur;
                }else{
                    s.push(cur);
                    cur = cur.right;
                    break;
                }
            }
        }while(!s.empty());
        return res;
    }
}

// 4. Time:O(n)  Space:O(1)
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        TreeNode dummy = new TreeNode(-1);
        dummy.left = root;
        TreeNode cur = dummy;
        TreeNode prev = null;
        while(cur!=null){
            if(cur.left==null){
                prev = cur;
                cur = cur.right;
            }else{
                TreeNode tmp = cur.left;
                while(tmp.right!=null && tmp.right!=cur){
                    tmp = tmp.right;
                }
                if(tmp.right==null){
                    tmp.right = cur;
                    prev = cur;
                    cur = cur.left;
                }else{
                    reverseVisit(cur.left,prev,res);
                    prev.right=null;
                    prev = cur;
                    cur = cur.right;
                }
            }
        }
        return res;
    }
    
    public void reverse(TreeNode from, TreeNode to){
        TreeNode cur = from;
        TreeNode suc = from.right;
        TreeNode tmp = null;
        if(from == to) return;
        while(cur!=to){
            tmp = suc.right;
            suc.right = cur;
            cur = suc;
            suc = tmp;
        }
    }
    
    public void reverseVisit(TreeNode from, TreeNode to, List<Integer> res){
        TreeNode tmp = to;
        reverse(from,to);
        while(true){
            res.add(tmp.val);
            if(tmp==from)
                break;
            tmp = tmp.right;
        }
        reverse(to,from);
    }
}
posted @ 2020-04-27 12:05  AAAmsl  阅读(62)  评论(0编辑  收藏  举报