68. Binary Tree Postorder Traversal
Description
Given a binary tree, return the postorder traversal of its nodes' values.
Example
Given binary tree {1 ,
1
\
2
/
3
return [3 ,2 ,1 ].
Solution
import java.util.*;
/**
* 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: A Tree
* @return : Postorder in ArrayList which contains node values.
*/
List<Integer> list = new ArrayList<Integer>();
public List<Integer> postorderTraversal (TreeNode root) {
if (root!=null ){
postorderTraversal(root.left);
postorderTraversal(root.right);
list.add(root.val);
}
return list;
}
}