Given the root
of a binary tree and a node u
in the tree, return the nearest node on the same level that is to the right of u
, or null
if u
is the rightmost node in its level.
If there is no node u
in the tree, return null
.
Consider the following binary tree:
1
/ \
2 3
/ \ \
4 5 6
u
is node 2
, the nearest right node is 3
.u
is node 4
, the nearest right node is 5
.u
is node 5
, the nearest right node is null
.u
is node 6
, the nearest right node is null
.u
is node 1
, the nearest right node is null
.u
is null
?When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method involves examining every single node in the tree until we locate the target node. Once found, we explore the tree level by level to find the nearest node to its right.
Here's how the algorithm would work step-by-step:
def find_nearest_right_node_brute_force(root, target):
if not root:
return None
target_node = None
queue = [root]
while queue:
node = queue.pop(0)
if node == target:
target_node = node
break
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
if not target_node:
return None
queue = [root]
while queue:
level_size = len(queue)
for i in range(level_size):
node = queue.pop(0)
# Found target node, check for right neighbor
if node == target:
if i + 1 < level_size:
return queue[0]
else:
pass
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return None
We need to find the node immediately to the right of a target node at the same level in a binary tree. The best way is to explore the tree level by level, so that when we find our target, the next node we see is our answer.
Here's how the algorithm would work step-by-step:
from collections import deque
def find_nearest_right_node(root, target):
if not root:
return None
queue = deque([root])
while queue:
level_length = len(queue)
# Iterate through all nodes at the current level
for i in range(level_length):
current_node = queue.popleft()
# Check if the current node is the target
if current_node == target:
# If it's the last node, return None
if i == level_length - 1:
return None
# Otherwise, the next node is the answer
else:
return queue[0]
# Add children to the queue for the next level
if current_node.left:
queue.append(current_node.left)
if current_node.right:
queue.append(current_node.right)
return None
Case | How to Handle |
---|---|
Root is null | Return null immediately as there are no nodes in the tree. |
Target node is null | Return null, as the search target is invalid. |
Target node is the rightmost node on its level | Return null because there is no right node on the same level. |
Target node is not present in the tree | Return null since no right neighbor exists for a non-existent target. |
Tree consists of only the root node (target is root) | Return null, as the root node has no siblings. |
Tree is a skewed tree (all nodes are left or right children) | If the target is in the skewed structure, its right neighbor will be null or not present in the level, returning null accordingly. |
Tree contains duplicate values, and target value appears multiple times | The solution should return the right neighbor of the first instance of the target node found at the target's level during a level order traversal. |
Very large tree exceeding memory constraints for level order traversal | Consider iterative deepening depth-first search approach with level information to reduce memory footprint. |