← DiffPush

Postorder Traversal of Binary Tree

Baseline Binary Trees · Traversals O(n) · O(n)

Demolition-Permit Walkthrough

A demolition contractor surveys a building shaped like a binary tree of structural bays. Safety law forbids logging a bay for teardown until every bay beneath it — the whole left wing and then the whole right wing — has already been inspected. Only after both wings are cleared does the crew sign off the bay itself. The signed-off sequence is what the permit office receives.

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: visit the left subtree, then the right subtree, then the node.

Constraints

Examples

Example 1

Input: {"tree":[1,2,3,4,5,null,6]}
Output: [4,5,2,6,3,1]
Left subtree finishes as 4,5,2; right subtree as 6,3; the root lands last.

Example 2

Input: {"tree":[7]}
Output: [7]
A single node has no children, so it is visited first and last.

Solve this in your browser →

Also on LeetCode ↗