Binary Tree Inorder Traversal

Easy
DFS

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

In an inorder traversal, we visit nodes in the following order:

  1. Recursively traverse the left subtree
  2. Visit the root node
  3. Recursively traverse the right subtree

For a Binary Search Tree (BST), inorder traversal returns nodes in sorted order.

Example Tree:

    1
     \
      2
     /
    3

Inorder traversal: [1, 3, 2]

Follow-up: Can you solve this iteratively using a stack?

Example 1

Input: root = [1,null,2,3]
Output: [1,3,2]
Explanation: Visit left (none), root (1), then right subtree. Right subtree: left (3), root (2), right (none).

Example 2

Input: root = []
Output: []
Explanation: Empty tree returns empty list.

Example 3

Input: root = [1]
Output: [1]
Explanation: Single node tree returns just that node.

Constraints

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100
Show Hints (4)
Hint 1: For recursive approach, what is the base case when the node is null?
Hint 2: The key insight is: process left subtree completely, then current node, then right subtree.
Hint 3: For iterative approach, use a stack to simulate the call stack. Go left as far as possible, then pop and go right.
Hint 4: Think about what you need to track: the current node and whether you've visited its left subtree.
Loading editor...

Test Results

Click "Run" to execute your code against test cases

AI Tutor

Socratic guidance - I'll ask questions, not give answers

AI Tutor
Hello! I'm your AI tutor, here to guide you through this problem using the Socratic method. I won't give you direct answers, but I'll ask questions and provide hints to help you discover the solution yourself. What's your first instinct when you look at this problem? What approach comes to mind?