← DiffPush

All in One Traversal

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

The Tri-Log Site Survey

A telecom crew surveys a relay network shaped like a binary tree and must file three reports from one walk: an engineering log that stamps relays after their left feed but before their right, a deployment log that stamps on arrival, and a decommissioning log that stamps only after both feed branches are cleared. Walking the tree once with all three counters running, each relay appends its ID to exactly one log per report.

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

Output: Return three arrays packed as one list: [inorder, preorder, postorder], in that order.

Constraints

Examples

Example 1

Input: {"tree":[1,2,3,4,5,null,6]}
Output: [[4,2,5,1,3,6],[1,2,4,5,3,6],[4,5,2,6,3,1]]
One walk, three ledgers: left-self-right, self-left-right, left-right-self.

Example 2

Input: {"tree":[7]}
Output: [[7],[7],[7]]
A single node produces the same one-element log in every order.

Solve this in your browser →

Also on LeetCode ↗