# LeetCode: Binary Tree Inorder Traversal Solution

Recursive or iterative (stack)

## Approach

Inorder traversal's process order:

• node.left
• node
• node.right

## Implementation (recursive)

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var inorderTraversal = function (root) {2  let res = []3
4  const recursion = node => {5    if (!node) return6
7    recursion(node.left)8    res.push(node.val)9    recursion(node.right)10
11    return12  }13
14  recursion(root)15
16  return res17}```

## Implementation (iterative)

```1var inorderTraversal = function (root) {2  const res = []3  const stack = []4
5  for (let node = root; node !== null || stack.length !== 0; ) {6    for (; node !== null; node = node.left) {7      stack.push(node)8    }9
10    node = stack.pop()11    res.push(node.val)12    node = node.right13  }14
15  return res16}```

## Comments

Loading comments...

leetcode

stack

tree

binary tree

## Next Post

LeetCode: Remove Duplicates from Sorted List

Aug 26, 2021

Keep jump over next node until values are not equal

## Previous Post

CSSBattle 1.11: Eye of Sauron

Aug 24, 2021

Pseudo-element, half circle

Search Posts