Advertisement

Flatten Binary Tree to Linked List - LeetCode 114 Solution

Flatten Binary Tree to Linked List - Complete Solution Guide

Flatten Binary Tree to Linked List is LeetCode problem 114, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the root of a binary tree, flatten the tree into a "linked list": The "linked list" should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null . The "linked list" should be in the same order as a pre-order traversal of the binary tree. 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] Con

Detailed Explanation

The problem asks us to flatten a given binary tree into a linked list structure. The linked list should be formed by performing a pre-order traversal of the binary tree. Importantly, the left child of each node in the linked list must be NULL, and the right child must point to the next node in the pre-order traversal. We need to modify the tree in-place, meaning we shouldn't create a new tree structure but rather rearrange the existing one.

Solution Approach

The solution uses an iterative approach to flatten the binary tree in-place. It iterates through the tree, and when it encounters a node with a left child, it finds the rightmost node in the left subtree. It then reattaches the original right subtree of the current node to the rightmost node of the left subtree. The left subtree of the current node is then moved to become the new right subtree, and the left child of the current node is set to null. This process effectively transforms the tree into a linked list representation in pre-order.

Step-by-Step Algorithm

  1. Step 1: Initialize a `current` pointer to the root of the tree.
  2. Step 2: While `current` is not null, continue the iteration.
  3. Step 3: If `current` has a left child:
  4. Step 4: Find the rightmost node (`predecessor`) in the left subtree of `current`.
  5. Step 5: Set the `right` child of `predecessor` to the `right` child of `current`.
  6. Step 6: Set the `right` child of `current` to its `left` child.
  7. Step 7: Set the `left` child of `current` to null.
  8. Step 8: Move `current` to its `right` child, and repeat from Step 2.

Key Insights

  • Insight 1: The key is to perform a pre-order traversal while restructuring the tree. The pre-order traversal order is Node -> Left -> Right.
  • Insight 2: We can use an iterative approach with O(1) space complexity, avoiding recursion's call stack overhead.
  • Insight 3: For each node, find the rightmost node of its left subtree. Connect this rightmost node to the current node's right subtree. Then move the left subtree to the right, and set the left to null.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Linked List, Stack, Tree, Depth-First Search, Binary Tree.

Companies

Asked at: Anduril, Apple, Google, Media.net, Myntra, PayPal, Salesforce, Uber, Yahoo.