Duplicate each node in a binary search tree and add as its left child
EasyUpdated: Aug 2, 2025
Problem
For each node in a binary search tree, create a duplicate of the node, and insert the duplicate as the left child of the original node. The updated tree should preserve its binary search property.
Examples
Example 1
Input:
2
/ \
1 3
Output:
2
/ \
2 3
/ /
1 3
/
1
Explanation:
Each node is duplicated as the left child:
- Node `2` gets a duplicate left child `2`.
- Node `1` gets a duplicate left child `1`.
- Node `3` gets a duplicate left child `3`.
Example 2
Input:
10
/ \
5 20
Output:
10
/ \
10 20
/ /
5 20
/
5
Explanation:
Each node is duplicated as the left child:
- Node `10` gets a duplicate left child `10`.
- Node `5` gets a duplicate left child `5`.
- Node `20` gets a duplicate left child `20`.
Solution
Method 1 - Using preorder traversal
The problem is exactly as described — for each node, a duplicate needs to be created and inserted as the left child. Importantly, the binary search tree property is maintained since the duplicate nodes appear as left children without disrupting the order of left (smaller values) and right (greater values) descendants.
Approach
- Traverse the tree using a depth-first search (DFS).
- At each node:
- Create a duplicate node with the same value.
- Insert this duplicate as the left child of the current node.
- Point the duplicate's left child to the original left subtree.
- Continue recursively on the original left and right children of the tree.
- Return the modified tree.
Code
Java
class Solution {
// Definition for a binary tree node
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
// Function to create duplicate nodes
public void duplicateTree(TreeNode root) {
if (root == null) {
return;
}
// Create duplicate node
TreeNode duplicate = new TreeNode(root.val);
// Attach duplicate as the left child
duplicate.left = root.left;
root.left = duplicate;
// Recursive calls for original left and right children
duplicateTree(duplicate.left);
duplicateTree(root.right);
}
// Helper function to print tree (inorder traversal)
public void print(TreeNode root) {
if (root == null) return;
print(root.left);
System.out.print(root.val + " ");
print(root.right);
}
public static void main(String[] args) {
Solution sol = new Solution();
TreeNode root = new TreeNode(2);
root.left = new TreeNode(1);
root.right = new TreeNode(3);
System.out.println("Original Tree:");
sol.print(root);
// Transform
sol.duplicateTree(root);
System.out.println("\nTransformed Tree:");
sol.print(root);
}
}
Python
class Solution:
def duplicateTree(self, root):
if root is None:
return
# Create duplicate node
duplicate = TreeNode(root.val)
# Attach duplicate as the left child
duplicate.left = root.left
root.left = duplicate
# Recursive calls for original left and right children
self.duplicateTree(duplicate.left)
self.duplicateTree(root.right)
def printTree(self, root):
# Helper function to print the tree (inorder traversal)
if root is None:
return
self.printTree(root.left)
print(root.val, end=" ")
self.printTree(root.right)
# Driver code
if __name__ == "__main__":
solution = Solution()
# Example tree
root = TreeNode(2)
root.left = TreeNode(1)
root.right = TreeNode(3)
print("Original Tree:")
solution.printTree(root)
# Transform the tree
solution.duplicateTree(root)
print("\nTransformed Tree:")
solution.printTree(root)
Complexity
- ⏰ Time complexity:
O(n)— Each node is visited once, and inserting duplicates isO(1). - 🧺 Space complexity:
O(h)— Recursion depth depends on the height of the tree (h).