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:
- Traverse the left subtree
- Visit the root node
- 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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code