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 OverviewInput Tree123Traversal Process1. No left child of 1 → Visit 12. Go right to 23. Left child of 2 → Visit 34. Back to 2 → Visit 25. No right child of 2Output Array132[1, 3, 2]Inorder: Left → Root → Right
Understanding the Visualization
1
Input Tree
Binary tree represented as array [1,null,2,3]
2
Traverse Inorder
Visit left subtree, root, then right subtree
3
Output Array
Collect node values in traversal order
Key Takeaway
🎯 Key Insight: Inorder traversal naturally produces sorted order for binary search trees
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