Skip to content
AI360Xpert
Beta
Easy

Binary Tree Inorder Traversal

Problem statement

Given the root of a binary tree, return the values of its nodes in inorder sequence: left subtree, then the node itself, then the right subtree.

Examples

Input:root = [1,null,2,3]
Output:[1,3,2]
Explanation:Node 1 has no left child so it comes first, then the right subtree yields 3 before 2.
Input:root = []
Output:[]
Explanation:An empty tree yields no values.
Input:root = [1]
Output:[1]
Explanation:A lone node is visited by itself.

Constraints

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

Hints

Hint 1
State the order as a mantra: left, node, right, applied at every subtree.
Hint 2
To drop the call stack, walk down the left spine yourself and climb back using saved ancestors.
Hint 3
For constant space, thread each left subtree back to its parent, then cut the thread on the second visit.
Edge Cases
  • An empty root returns an empty list without visiting anything.
  • A skewed chain exercises only one side at each step, which both methods handle without extra logic.
Common Mistakes
  • Recording the node before the left subtree, which produces preorder instead of inorder.
  • In Morris traversal, forgetting to remove a thread, which loops forever around the same subtree.
  • Finding the wrong predecessor by stopping at any right child instead of the rightmost one lacking a thread.
Pattern Takeaway
Tree orderings are just visit schedules; recursion schedules with the call stack, and threading schedules with the tree itself.

Solutions

Solution Details

Reveal Recursive Traversal: intuition, complexity, and code