You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree is defined as follows:
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
. Initially, all next pointers are set to NULL
.
You need to write code that connects each node to its next right node.
Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like the diagram in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
Node
objects.Node
class structure cannot be changed?
Node
class structure cannot be changed.next
pointers.class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
def connect(root: 'Node') -> 'Node':
if not root:
return root
# Start with the root
leftmost = root
# Traverse levels
while leftmost.left:
# Go through the current level
head = leftmost
while head:
# Connect the children of the current node
head.left.next = head.right
# Connect right child to the next node's left child
if head.next:
head.right.next = head.next.left
# Move to the next node
head = head.next
# Move to the next level
leftmost = leftmost.left
return root
# Example Usage
# root = Node(1, Node(2, Node(4), Node(5)), Node(3, Node(6), Node(7)))
# connect(root)
The time complexity for this algorithm is O(N) where N is the number of nodes in the tree. Each node is traversed exactly once, and the connections are made in constant time.
The space complexity is O(1) because we use no additional data structures that scale with input size, only a few pointers.
This solution efficiently connects all the nodes using their next
pointers as required.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?