---
# System prepended metadata

title: LC 94. Binary Tree Inorder Traversal
tags: [easy, Binary Tree, leedcode, python, c++]

---

# LC 94. Binary Tree Inorder Traversal

### [Problem link](https://leetcode.com/problems/binary-tree-inorder-traversal/)

###### tags: `leedcode` `easy` `python` `c++` `Binary Tree`

Given the <code>root</code> of a binary tree, return the inorder traversal of its nodes' values.

**Example 1:** 
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg" style="width: 125px; height: 200px;" />
```
Input: root = [1,null,2,3]
Output: [1,3,2]
```

**Example 2:** 

```
Input: root = []
Output: []
```

**Example 3:** 

```
Input: root = [1]
Output: [1]
```

 **Constraints:** 

- The number of nodes in the tree is in the range <code>[0, 100]</code>.
- <code>-100 <= Node.val <= 100</code>


 **Follow up:**  Recursive solution is trivial, could you do it iteratively?
## Solution 1
#### Python
##### recursive
```python=
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        res = []

        def traversal(node: TreeNode):
            if not node:
                return

            traversal(node.left)
            res.append(node.val)
            traversal(node.right)

        traversal(root)

        return res
```
##### iteration
```python=
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        res = []
        stack = []

        if root:
            stack.append(root)
        while stack:
            node = stack.pop()
            if node != None:
                if node.right:
                    stack.append(node.right)
                stack.append(node)
                stack.append(None)
                if node.left:
                    stack.append(node.left)
            else:
                node = stack.pop()
                res.append(node.val)

        return res
```

#### C++
##### recursive
```cpp=
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> res;

    void traversal(TreeNode *cur) {
        if (cur == nullptr) {
            return;
        }
        traversal(cur->left);
        res.push_back(cur->val);
        traversal(cur->right);
    }

    vector<int> inorderTraversal(TreeNode* root) {
        traversal(root);
        return res;
    }
};
```
##### iteration
```cpp=
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> st;

        if (root != nullptr) {
            st.push(root);
        }
        while (!st.empty()) {
            TreeNode *node = st.top();
            st.pop();

            if (node != nullptr) {
                if (node->right) {
                    st.push(node->right);
                }
                st.push(node);
                st.push(nullptr);
                if (node->left) {
                    st.push(node->left);
                }
            } else {
                node = st.top();
                st.pop();
                res.push_back(node->val);
            }
        }
        return res;
    }
};
```

>### Complexity
>n = number of tree's nodes.
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n)            | O(n)             |

## Note
[iteration reference](https://github.com/youngyangyang04/leetcode-master/blob/master/problems/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E7%BB%9F%E4%B8%80%E8%BF%AD%E4%BB%A3%E6%B3%95.md)
[recursive reference](https://github.com/youngyangyang04/leetcode-master/blob/master/problems/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E8%BF%AD%E4%BB%A3%E9%81%8D%E5%8E%86.md)