145. 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]
.
Note: Recursive solution is trivial, could you do it iteratively?
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> list= new LinkedList<>();
if (root==null) return list;
Stack<TreeNode> stack= new Stack<>();
stack.push(root);
while (!stack.isEmpty()){
TreeNode t= stack.pop();
list.addFirst(t.val);
if (t.left!=null) stack.push(t.left);
if (t.right!=null) stack.push(t.right);
}
return list;
}
}