Example 1:
If we are given a linked list that contains nodes in heap order such as 1 -> 4 -> 5 -> 6 -> 8 -> 10 -> 11-> null then we want to convert it to a balanced binary tree as follows -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Input: head =[3 -> 2 -> 1 -> 4 -> 5 ->null]Output:
3
/\ 2 1
/\ 4 5
Explanation:
The queue-based approach creates the binary tree level by level:
- Add 1 to the queue as the root.
- Dequeue 1, attach 2 (left) and 3 (right), and enqueue both.
- Dequeue 2, attach 4 (left) and 5 (right), and enqueue them.
To solve the problem of converting a linked list representation of a heap into a binary tree (BT), we can follow the straightforward approach of constructing the binary tree level by level based on the heap properties stored in the linked list.
In a heap (especially a complete binary heap), nodes at position i in the array have:
classSolution {
public TreeNode convertToBT(ListNode head) {
if (head ==null) returnnull;
// Step 1: Convert LinkedList to Array of TreeNode List<TreeNode> nodes =new ArrayList<>();
while (head !=null) {
nodes.add(new TreeNode(head.val));
head = head.next;
}
// Step 2: Link nodes to mimic binary tree structure (heap-like linkage)for (int i = 0; i < nodes.size(); i++) {
if (2 * i + 1 < nodes.size()) { // Left Child index nodes.get(i).left= nodes.get(2 * i + 1);
}
if (2 * i + 2 < nodes.size()) { // Right Child index nodes.get(i).right= nodes.get(2 * i + 2);
}
}
return nodes.get(0); // Root of the binary tree }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defconvert_to_bt(self, head: Optional[ListNode]) -> Optional[TreeNode]:
ifnot head:
returnNone# Step 1: Convert Linked List to Array of TreeNode nodes = [] # Array to hold TreeNode objectswhile head:
nodes.append(TreeNode(head.val))
head = head.next
# Step 2: Link nodes to mimic binary tree structure (heap-like linkage)for i in range(len(nodes)):
if2* i +1< len(nodes): # Left Child index nodes[i].left = nodes[2* i +1]
if2* i +2< len(nodes): # Right Child index nodes[i].right = nodes[2* i +2]
return nodes[0] # Root of the binary tree
Using a queue is a great technique for level-order traversal and is perfectly suited to create a binary tree from the linked list representation of a heap.
classSolution {
public TreeNode convertToBT(ListNode head) {
if (head ==null) returnnull;
// Create the root node from the first linked list node TreeNode root =new TreeNode(head.val);
Queue<TreeNode> queue =new LinkedList<>();
queue.add(root); // Queue for level-order construction head = head.next; // Move to the next node in the linked list// Construct the binary tree using the queuewhile (head !=null) {
TreeNode parent = queue.poll(); // Get parent node from the queue// Create left child if available TreeNode leftChild =new TreeNode(head.val);
parent.left= leftChild;
queue.add(leftChild);
head = head.next;
// Create right child if availableif (head !=null) {
TreeNode rightChild =new TreeNode(head.val);
parent.right= rightChild;
queue.add(rightChild);
head = head.next;
}
}
return root;
}
}
classSolution:
defconvert_to_bt(self, head: Optional[ListNode]) -> Optional[TreeNode]:
ifnot head:
returnNone# Create the root node from the first linked list node root = TreeNode(head.val)
queue = deque([root]) # Queue for level-order construction head = head.next # Move to the next node in the linked list# Construct the binary tree using the queuewhile head:
parent = queue.popleft() # Get parent node# Create left child if available left_child = TreeNode(head.val)
parent.left = left_child
queue.append(left_child)
head = head.next
# Create right child if availableif head: # Ensure there's still a node left right_child = TreeNode(head.val)
parent.right = right_child
queue.append(right_child)
head = head.next
return root