Binary Tree Postorder Traversal

2015-01-22 20:51:10 · 作者: · 浏览: 12

?

?

?

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?

?

?

思路:

?

(1)题意为后序遍历二叉树。遍历顺序为左—>右—>根。

(2)考虑到用递归比较简单,本文使用递归的思想进行解决,由于比较简单这里不累赘,详见下方代码。

(3)希望本文对你有所帮助。


?

?

?

算法代码实现如下:

/**
 * @author liqq
 */
public List
  
    PostorderTraversal(TreeNode root) {
	List
   
     result = new LinkedList
    
     (); if (root != null) { Post_order(result, root.left); Post_order(result, root.right); result.add(root.val); } return result; } private void Post_order(List
     
       result, TreeNode curr) { if (curr != null) { Post_order(result, curr.left); Post_order(result, curr.right); result.add(curr.val); } }
     
    
   
  


?