← DiffPush

Iterative Postorder Traversal of Binary Tree

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

The Return-Visit Warranty Clerk

A warranty clerk walks a dealership's branch chart loop-only: no recursive paperwork chains permitted. She parks nodes on her desk pile while sliding left, but a station may only be signed off once its right branch is either empty or already stamped — she keeps a seen-it ledger to tell the difference. If the right branch still needs work, it goes on the pile and the loop slides left through it first.

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 postorder, produced without recursion.

Constraints

Examples

Example 1

Input: {"tree":[1,2,3,4,5,null,6]}
Output: [4,5,2,6,3,1]
Children always land before their parents; the root closes the log.

Example 2

Input: {"tree":[7]}
Output: [7]
With no right branch pending, the lone node is stamped on the first peek.

Solve this in your browser →

Also on LeetCode ↗