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?
给定二叉树,返回其节点值的后序遍历。
import java.util.*;;
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
ArrayList<Integer> num=new ArrayList<Integer>();
public ArrayList<Integer> postorderTraversal(TreeNode root) {
if(root == null){
return num;
}
postorderTraversal(root.left);
postorderTraversal(root.right);
num.add(root.val);
return num;
}
}