> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/01-05-2022-145.md).

# 01/05/2022 145

145 postorder binary tree

O(n) O(height)

```
    
    
    class Solution { 
    public List postorderTraversal(TreeNode root) { 
    Stack<TreeNode> stack = new Stack<>(); 
    List<Integer> result = new ArrayList<>(); 
    while (!stack.isEmpty() || root != null){ 
    while(root != null){ stack.push(root); 
    if(root.left != null){ 
        root = root.left; 
    } else{ root = root.right; } 
 }
     
     TreeNode node = stack.pop();
     result.add(node.val);
     
     if(!stack.isEmpty() && stack.peek().left == node){
         root = stack.peek().right; // need to remember here.
     }
         
     
 }
    
return result;
 } 
}
```
