2017-06-03 21:31:49 +0000   |     algorithm leetcode binary tree tree stack   |   Viewed times   |    

题目

Given a binary tree, return the postorder traversal of its nodes’ values.

For example: Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

递归版

Binary Tree的问题,递归法是最简单的。

代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) { return res; }
        res.addAll(postorderTraversal(root.left));
        res.addAll(postorderTraversal(root.right));
        res.add(root.val);
        return res;
    }
}

结果

binary-tree-postorder-traversal-1

迭代版

Post Order的迭代不像Pre Order的迭代这么直观地从右往左遍历。而是 相反地从左往右遍历。还是要用一个Stack缓存节点。以下面的树为例,

     1
   /   \
  2     3
 / \   / \
4   5 6   7
  1. Stack开头压入1
  2. Stack读取1res开头压入1
  3. Stack开头压入2, 开头压入3
  4. Stack读取3res开头压入3
  5. Stack开头压入6,开头压入7

代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        Deque<TreeNode> buffer = new LinkedList<TreeNode>();
        buffer.offerFirst(root);
        while (!buffer.isEmpty()) {
            TreeNode node = buffer.pollFirst();
            if (node != null) {
                buffer.offerFirst(node.left);
                buffer.offerFirst(node.right);
                res.add(0,node.val);
            }
        }
        return res;
    }
}

结果

binary-tree-postorder-traversal-2