Algo-Sim › Binary Trees & BST
Flatten Binary Tree to Linked List
Medium · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Rewire the tree in place into a right-pointing list in preorder: splice each left subtree between a node and its right subtree, using O(1) extra space.
Problem
Flatten a binary tree in place into a "linked list" that uses the same nodes: each node’s right points to the next node in preorder and every left is null.
Constraints
- 0 ≤ number of nodes ≤ 2000
- -100 ≤ Node.val ≤ 100
Edge cases to test
- Empty tree
- Only right children (already flat)
- Only left children
Follow-up questions
- Can you avoid recursion?
- Morris-style: for each node with a left child, find the rightmost node of the left subtree, hook the right subtree there, and move left to right: O(n), O(1).
- What is the reverse-preorder trick?
- Visit right, left, node and set
node.right = prev,node.left = null: one short recursion.
Solution
Java
public void flatten(TreeNode root) {
TreeNode node = root;
while (node != null) {
if (node.left != null) {
TreeNode tail = node.left;
while (tail.right != null) tail = tail.right;
tail.right = node.right;
node.right = node.left;
node.left = null;
}
node = node.right;
}
}Python
def flatten(root):
node = root
while node:
if node.left:
tail = node.left
while tail.right:
tail = tail.right
tail.right = node.right
node.right, node.left = node.left, None
node = node.rightC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void flatten(TreeNode *root) {
TreeNode *node = root;
while (node != nullptr) {
if (node->left != nullptr) {
TreeNode *tail = node->left;
while (tail->right != nullptr)
tail = tail->right;
tail->right = node->right;
node->right = node->left;
node->left = nullptr;
}
node = node->right;
}
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)