Medium
Given the root
of a binary tree, flatten the tree into a “linked list”:
TreeNode
class where the right
child pointer points to the next node in the list and the left
child pointer is always null
.Example 1:
Input: root = [1,2,5,3,4,null,6]
Output: [1,null,2,null,3,null,4,null,5,null,6]
Example 2:
Input: root = []
Output: []
Example 3:
Input: root = [0]
Output: [0]
Constraints:
[0, 2000]
.-100 <= Node.val <= 100
Follow up: Can you flatten the tree in-place (with O(1)
extra space)?
To solve the “Flatten Binary Tree to Linked List” problem in Python with a Solution
class, we’ll use a recursive approach. Below are the steps:
Create a Solution
class: Define a class named Solution
to encapsulate our solution methods.
Create a flatten
method: This method takes the root node of the binary tree as input and flattens the tree into a linked list using preorder traversal.
Check for null root: Check if the root is null. If so, there’s no tree to flatten, so return.
flattenTree
to perform the flattening.
flattenTree
method with the root node.Here’s the Python implementation:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def flatten(self, root: TreeNode) -> None:
"""
Do not return anything, modify root in-place instead.
"""
if root:
self.find_tail(root)
def find_tail(self, root: TreeNode) -> TreeNode:
left = root.left
right = root.right
# Find the tail of the left subtree, tail means the most left leaf
if left:
tail = self.find_tail(left)
# Stitch the right subtree below the tail
root.left = None
root.right = left
tail.right = right
else:
tail = root
# Find tail of the right subtree
if tail.right is None:
return tail
else:
return self.find_tail(tail.right)
This implementation follows the steps outlined above and efficiently flattens the binary tree into a linked list using preorder traversal in Python.