zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    
  • 相关阅读:
    20200924-3 单元测试,结对
    20200924-1 每周例行报告
    20200924-5 四则运算试题生成,结对
    20200924-2 功能测试
    20200924-4 代码规范,结对要求
    20200929-git地址
    20200917-1 每周例行报告
    20200917-2 词频统计
    20200917-3 白名单
    20200910-2 博客作业
  • 原文地址:https://www.cnblogs.com/AAAmsl/p/12785812.html
Copyright © 2011-2022 走看看