首页 > 代码库 > Binary Tree Postorder Traversal

Binary Tree Postorder Traversal

1. Recursive

Straight Forward, left -> right -> root

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Postorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> rst = new ArrayList<Integer>();
        if (root == null) {
            return rst;
        }
        traversal(rst, root);
        return rst;
    }
    
    private void traversal(ArrayList<Integer> rst, TreeNode root) {
        if (root == null) {
            return;
        }    
        traversal(rst, root.left);
        traversal(rst, root.right);
        rst.add(root.val);
    }
}

 

2. Iterator

This is a little bit hard to understand, here is good reference https://sites.google.com/site/jennyshelloworld/company-blog/chapter-3---binary-tree-divide-conquer

The key to iterative postorder traversal is the following: 

  1. The order of "Postorder" is: left child -> right child -> parent node. 
  2. Find the relation between the previously visited node and the current node
  3. Use a stack to track nodes

As we go down the tree, check the previously visited node. If it is the parent of the current node, we should add current node to stack. When there is no children for current node, pop it from stack. Then the previous node become to be under the current node for next loop.

我们使用prev变量跟踪上一次访问的节点。假设栈顶元素是curr。当prev是curr的父节点时,我们正在向下遍历树。此时,优先遍历curr的左孩子(将左孩子压入栈)。如果没有左孩子,再看右孩子。如果左右孩子都不存在(curr是叶节点),就输出curr的值并弹出栈顶元素。

如果prev是curr的左孩子,我们正在从左子树向上遍历。我们看一下curr的右孩子。如果可以,就从右孩子向下遍历(将右孩子压入栈),否则打印curr的值并弹出栈顶元素。

如果prev是curr的右孩子,我们正在从右子树向上遍历。打印curr的值并弹出栈顶元素。

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Postorder in ArrayList which contains node values.
     */
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        // write your code here
        ArrayList<Integer> rst = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode cur = root;
        TreeNode prev = null;
        
        if (root == null) {
            return rst;
        }
        
        stack.push(cur);
        while (!stack.empty()) {
            cur = stack.peek();
            if (prev == null || prev.left == cur || prev.right == cur) {
                if (cur.left != null) {
                    stack.push(cur.left);
                } else if (cur.right != null) { 
                // ‘else if‘ make sure all the left child will be processed first
                    stack.push(cur.right);
                }
            } else if (cur.left == prev) {
                if (cur.right != null) {
                    stack.push(cur.right);
                }
            } else {
                rst.add(cur.val);
                stack.pop();
            }
            prev = cur;
        }
        return rst;
    }
}

 

Binary Tree Postorder Traversal