← DiffPush

Iterative Inorder Traversal of Binary Tree

Standard Bar Binary Trees · Traversals O(n) · O(n)

The Escalator Inspector's Left Slide

A mall maintenance bot inspects a tree of escalators without any recursive subroutines — its firmware only allows a loop and a work-pile. Its trick: slide leftward as far as possible, parking every landing on the pile, then service the top landing, and re-emerge on its right branch to repeat. Landings get stamped exactly between their two descents, matching the official left-self-right sweep sheet.

Input: The root of a binary tree given as a level-order array where null marks a missing child.

Output: Return an array of node values in inorder, produced without recursion.

Constraints

Examples

Example 1

Input: {"tree":[1,2,3,4,5,null,6]}
Output: [4,2,5,1,3,6]
Left wing bottoms out at 4, unwinds 4,2,5, then the root, then 3,6.

Example 2

Input: {"tree":[7]}
Output: [7]
The single node is parked, popped, and recorded in one pass.

Solve this in your browser →

Also on LeetCode ↗