• 145. 二叉树的后序遍历


    递归

    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {   
            List<Integer> ls = new ArrayList<Integer>();
    		end(root, ls);
    		return ls;
    	}
    
    	public static void end(TreeNode T, List<Integer> ls) {
    		if (T == null)
    			return;
            end(T.left, ls);
            end(T.right, ls);
    		ls.add(T.val);
    	}
    }
    

    递推有点难

    public List<Integer> postorderTraversal(TreeNode root) {
    		LinkedList<TreeNode> ls = new LinkedList<>();
    		LinkedList<Integer> output = new LinkedList<>();
    		if (root == null) {
    			return output;
    		}
    		ls.add(root);
    		while (!ls.isEmpty()) {
    			TreeNode node = ls.pollLast();
    			output.addFirst(node.val);
    			if (node.left != null) {
    				ls.add(node.left);
    			}
    			if (node.right != null) {
    				ls.add(node.right);
    			}
    		}
    		return output;
    	}
    }
    作者:LeetCode
    链接:https://leetcode-cn.com/problems/two-sum/solution/er-cha-shu-de-hou-xu-bian-li-by-leetcode/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root == null) return res;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode last = null;
        
        while(cur != null || !stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.peek();
            if(cur.right == null || cur.right == last){
                res.add(cur.val);
                stack.pop();
                last = cur;
                cur = null;
            }else{
                cur = cur.right;
            }
        }
        return res;
    }
    
    }
    
    
    public List<Integer> postorderTraversal(TreeNode root) {
            List<Integer> list = new ArrayList<>();
            Stack<TreeNode> stk1 = new Stack<>();
            Stack<Character> stk2 = new Stack<>();
            while(root!=null || !stk1.isEmpty()){
                while(root!=null){
                    stk1.push(root);
                    stk2.push('n');//n=>no=>访问的非当前节点
                    root = root.left;
                }
                while(!stk1.isEmpty() && stk2.peek()=='y')//y=>yes=>访问的当前节点
                {
                    list.add(stk1.pop().val);
                    stk2.pop();
                }
                if(!stk1.isEmpty()){
                    stk2.pop();
                    stk2.push('y');
                    root = stk1.peek();
                    root = root.right;
                }
            }
            return list;
        }
    
    
  • 相关阅读:
    POJ 1887 Testing the CATCHER
    HDU 3374 String Problem
    HDU 2609 How many
    POJ 1509 Glass Beads
    POJ 1458 Common Subsequence
    POJ 1159 Palindrome
    POJ 1056 IMMEDIATE DECODABILITY
    POJ 3080 Blue Jeans
    POJ 1200 Crazy Search
    软件体系结构的艺术阅读笔记1
  • 原文地址:https://www.cnblogs.com/cznczai/p/11180083.html
Copyright © 2020-2023  润新知