LeetCode 145: Binary Tree Postorder Traversal

Posted aaamsl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    }
}

以上是关于LeetCode 145: Binary Tree Postorder Traversal的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 145. Binary Tree Postorder Traversal

LeetCode 145:Binary Tree Postorder Traversal

leetcode145. Binary Tree Postorder Traversal

LeetCode 145: Binary Tree Postorder Traversal

leetcode No145. Binary Tree Postorder Traversal

LeetCode OJ 145. Binary Tree Postorder Traversal