Binary Tree Inorder Traversal

Easy

Topics
TreeDFSStack

Given the root of a binary tree, return its inorder traversal (left, node, right) as a list of values.

Example 1

113223
visited first
The small number above each node is its position in the output: left subtree, node, right subtree, giving [1, 3, 2]. For a BST this is the one order that comes out sorted. The iterative version swaps the call stack for an explicit one.
Tree given in level-order (breadth-first); null marks a missing child.
Input:  root = [1,null,2,3]
Output: [1,3,2]

Example 2

empty tree
Input:  root = []
Output: []

Constraints

  • 0 <= number of nodes <= 100
  • -100 <= Node.val <= 100
Editorial — intuition, approach, complexity and pitfalls

Intuition

Inorder means: fully visit the left subtree, then the node, then the right subtree. Recursion expresses this directly; an explicit stack gives the iterative version.

Approach

  1. Recurse left.
  2. Append the current node’s value.
  3. Recurse right.

Dry Run

        1
      /   \
     2     3
    / \
   4   5
  at 1: go left
    at 2: go left
      at 4: go left   -> null
      at 4: VISIT                      res [4]
      at 4: go right  -> null
    at 2: VISIT                        res [4, 2]
    at 2: go right
      at 5: go left   -> null
      at 5: VISIT                      res [4, 2, 5]
      at 5: go right  -> null
  at 1: VISIT                          res [4, 2, 5, 1]
  at 1: go right
    at 3: go left     -> null
    at 3: VISIT                        res [4, 2, 5, 1, 3]
    at 3: go right    -> null
answer 4 2 5 1 3
The node is appended at the moment control comes back from its left child and
before it descends right. That is the whole content of the word "inorder", and it
is why the root lands in the MIDDLE of the output rather than at either end.
Move res.append above dfs(node.left) and these same three lines emit 1 2 4 5 3,
which is preorder.
Worth noticing in the trace: 5 nodes but 11 calls. Every absent child still costs
a call that returns immediately, so an n-node tree makes n + 1 null calls.

Complexity

  • Time — O(n): each node visited once.
  • Space — O(h): recursion (or explicit) stack up to height h.

Common Pitfalls

  • The visit sits between the two recursive calls. Moving res.append above dfs(node.left) gives preorder, not inorder -- the same three lines in a different order return 1 2 4 5 3 where inorder gives 4 2 5 1 3.
  • Inorder on a BST is the sorted order. That is the property most problems are really reaching for, and it is worth saying so rather than treating the traversal as an end in itself.

Full Solution — all languages

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):
        res = []
        def dfs(node):
            if not node:
                return
            dfs(node.left)
            res.append(node.val)
            dfs(node.right)
        dfs(root)
        return res
SensAI
Beta
Tap the mic to stop voice mode