---
# System prepended metadata

title: 145. Binary Tree Postorder Traversal
tags: [' bt', tree]

---

---
link: https://leetcode.com/problems/binary-tree-postorder-traversal/
tags: tree, bt
---

# 145. Binary Tree Postorder Traversal

## Question
Given a binary tree, return the *postorder* traversal of its nodes' values.

**Example:**

```
Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [3,2,1]
```

**Follow up:** Recursive solution is trivial, could you do it iteratively?

## Solution: Python
```python=
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def postorderTraversalRecursive(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        result = []
        self.helper(root, result)
        return result
    
    def helper(self, node, result):
        if node is None:
            return
        
        self.helper(node.left, result)
        self.helper(node.right, result)
        result.append(node.val)
    
    def postorderTraversal(self, root):
        from collections import deque
        stack = deque()
        stack.append((False, root))  # IsAppendingResult, Node
        result = []
        
        while stack:
            is_appending_result, node = stack.pop()
            
            if node is None:
                continue
                
            if is_appending_result:
                result.append(node.val)
            else:
                stack.extend([(True, node), (False, node.right), (False, node.left)])
        return result
```

## Solution: Java
```java=

```