[LeetCode #145] Binary Tree Postorder Traversal

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 {
    	public void postorderTraversal(TreeNode root, java.util.List<Integer>  result) {
		if ( root == null) {
			return;
		}		
		postorderTraversal( root.left , result);
		postorderTraversal( root.right, result); 

		result.add(root.val);
	}
	
	
	public java.util.List<Integer> postorderTraversal(TreeNode root) {		
		java.util.List<Integer> result = new java.util.ArrayList<Integer>();
		postorderTraversal(root, result);
		return result;

	}

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s