Example 1
- Input
- root = [1,null,2,3]
- Output
- [1,3,2]
Node 1 has no left child, so append 1 before entering its right subtree. Node 2 has left child 3, which must be appended before 2. Thus the order is [1,3,2], not the level order [1,2,3].
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.
Given the root of a binary tree, return the inorder traversal of its node values.
Inorder means visiting the left subtree first, then the node itself, then the right subtree, repeating that order at every node.
Node 1 has no left child, so append 1 before entering its right subtree. Node 2 has left child 3, which must be appended before 2. Thus the order is [1,3,2], not the level order [1,2,3].
An empty root has neither a node to append nor subtrees to traverse. Its inorder sequence is an empty list.
Both subtrees of the lone node are empty, so append its value exactly once and return [1].
The left subtree contributes [1,2,3], then root 4 is appended, then the right subtree contributes [5]. Morris traversal must restore both temporary links, from 1 to 2 and from 3 to 4.
This input is an arbitrary binary tree, not necessarily a search tree. The left leaf 2 precedes root 2 and right leaf 1; duplicate values remain separate entries and the output need not be sorted.
Inorder describes where to place each node relative to its two subtrees: everything on its left comes before it, and everything on its right comes after it. In Example 1, node 1 has no left subtree, so it comes first; node 2 must wait for its left child 3, giving [1,3,2]. Recursion remembers that unfinished visit on the call stack. Morris traversal temporarily borrows an unused right-child link to remember the return route, then restores it.
Reveal Recursive Traversal: intuition, complexity, and code
Tree orderings are just visit schedules; recursion schedules with the call stack, and threading schedules with the tree itself.