Given the root of a binary tree and a node u in the tree, return thenearest node on the same level that is to the right ofu, or returnnullifuis the rightmost node in its level.

Input: root =[1,2,3,null,4,5,6], u =4Output: 5Explanation: The nearest node on the same level to the right of node 4is node 5.
Example 2:
1
2
3
4

Input: root =[3,null,4,2], u =2Output: nullExplanation: There are no nodes to the right of 2.
Constraints:
The number of nodes in the tree is in the range [1, 105].
To find the nearest right node of u on the same level, we can perform a level order traversal (BFS) and, for each level, look for u. If we find u, the next node in the queue (if any) is the answer.
classTreeNode:
def__init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
classSolution:
deffindNearestRightNode(self, root: TreeNode, u: TreeNode) -> TreeNode |None:
from collections import deque
q = deque([root])
while q:
sz = len(q)
for i in range(sz):
node = q.popleft()
if node == u:
returnNoneif i == sz-1else q[0]
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
returnNone