Binary Tree Inorder Traversal - Problem

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

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

  1. Traverse the left subtree
  2. Visit the root node
  3. Traverse the right subtree

For example, given the tree [1,null,2,3], the inorder traversal would be [1,3,2].

Input & Output

Example 1 — Simple Tree
$ Input: root = [1,null,2,3]
Output: [1,3,2]
💡 Note: Inorder traversal: no left child of 1, visit 1, then go right to 2. For node 2: visit left child 3 first, then visit 2, no right child. Result: [1,3,2]
Example 2 — Empty Tree
$ Input: root = []
Output: []
💡 Note: Empty tree has no nodes to traverse, so return empty array
Example 3 — Single Node
$ Input: root = [1]
Output: [1]
💡 Note: Single node tree: no left child, visit root 1, no right child. Result: [1]

Constraints

  • The number of nodes in the tree is in the range [0, 100]
  • -100 ≤ Node.val ≤ 100

Visualization

Tap to expand
Binary Tree Inorder Traversal INPUT Binary Tree Structure: 1 2 3 null Input Array: [1, null, 2, 3] ALGORITHM STEPS 1 Start at Root (1) Go left first (null) 2 Visit Node 1 Add 1 to result 3 Go Right to Node 2 Traverse left subtree (3) 4 Visit Node 3 Add 3 to result 5 Visit Node 2 Add 2 to result Order: Left --> Root --> Right Stack: [recursive calls] FINAL RESULT Visit Order: 1 1 2 3 3 2 Output Array: [1, 3, 2] OK - Inorder Complete Key Insight: Inorder traversal follows LEFT --> ROOT --> RIGHT pattern. For BSTs, this produces sorted output. Time Complexity: O(n) where n is number of nodes. Space Complexity: O(h) for recursion stack (h = height). TutorialsPoint - Binary Tree Inorder Traversal | Optimal Solution (Recursive)
Asked in
Facebook 52 Microsoft 38 Amazon 31 Google 28
892.0K Views
High Frequency
~15 min Avg. Time
11.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen