Flatten Binary Tree to Linked List - aloalgo

Flatten Binary Tree to Linked List

Medium

You are given the root of a binary tree.

Your task is to flatten the tree into a "linked list". This "linked list" should utilize the same TreeNode class. In this transformed structure, the right child pointer of each node should point to the subsequent node in the list, and the left child pointer must always be null. The order of the nodes in the "linked list" must exactly match the pre-order traversal sequence of the original binary tree.

Example 1

Input
123
Output
123
Explanation:

The tree has three nodes with values 1, 2, and 3. The flattened linked list will have the same values in pre-order traversal order.

Example 2

Input
None
Output
None
Explanation:

An empty tree contains no elements, so the result is also empty.

Loading...
Input
123
Output
123

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!