686. Flatten Binary Tree to Linked List

HardStackStackTree

Given a binary tree in level-order array form (using null for missing children), flatten it into a linked list following the tree's preorder traversal, where each node's right pointer is the next preorder node and left pointers are null. Return the resulting sequence of values. The input is JSON {tree}.

Input: JSON {tree}.

Output: Array — the preorder sequence after flattening.

Examples

Example 1
Input: {"tree":[1,2,5,3,4,null,6]}
Output: [1,2,3,4,5,6]
Explanation: Preorder traversal order.
Example 2
Input: {"tree":[0]}
Output: [0]
Explanation: Single node.
Example 3
Input: {"tree":[1,null,2,null,3]}
Output: [1,2,3]
Explanation: Already right-leaning.

Constraints

Asked by

MicrosoftBloombergAmazonOracleMetaGoogle
Solve this problem in the editor →