-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1602-find-nearest-right-node-in-binary-tree.js
More file actions
42 lines (36 loc) · 1.11 KB
/
1602-find-nearest-right-node-in-binary-tree.js
File metadata and controls
42 lines (36 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* 1602. Find Nearest Right Node in Binary Tree
* https://leetcode.com/problems/find-nearest-right-node-in-binary-tree/
* Difficulty: Medium
*
* 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 return null if u is the rightmost node in its level.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} u
* @return {TreeNode}
*/
var findNearestRightNode = function(root, u) {
const queue = [root];
while (queue.length > 0) {
const levelSize = queue.length;
for (let i = 0; i < levelSize; i++) {
const currentNode = queue.shift();
if (currentNode === u) {
return i === levelSize - 1 ? null : queue.shift();
}
if (currentNode.left) queue.push(currentNode.left);
if (currentNode.right) queue.push(currentNode.right);
}
}
return null;
};