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].
public class PostorderTraversal {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
if(root==null) return result; //返回[]
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode tmp = stack.peek();
if(tmp.left==null && tmp.right==null) {
stack.pop();
result.add(tmp.val);
} else {
if(tmp.right!=null) {
stack.push(tmp.right);
tmp.right=null;
}
if(tmp.left!=null) {
stack.push(tmp.left);
tmp.left=null;
}
}
}
return result;
}
public List<Integer> postorderTraversal2(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
if(root==null) return result; //返回[]
Stack<TreeNode> stack = new Stack<TreeNode>();
HashSet<TreeNode> hs = new HashSet<TreeNode>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode tmp = stack.pop();
if(hs.contains(tmp)){
result.add(tmp.val);
continue;
}
hs.add(tmp);
stack.push(tmp);
if(tmp.right!=null) {
stack.push(tmp.right);
}
if(tmp.left!=null) {
stack.push(tmp.left);
}
}
return result;
}
}