Binary Tree Postorder Traversal

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].

 

递归遍历比较简单,代码如下:

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

 

posted @ 2015-03-25 00:07  mrpod2g  阅读(90)  评论(0编辑  收藏  举报