Skip to content

Commit 36ac484

Browse files
authored
Create 1602-find-nearest-right-node-in-binary-tree.js
1 parent 32fc53c commit 36ac484

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @param {TreeNode} u
12+
* @return {TreeNode}
13+
*/
14+
const findNearestRightNode = function(root, u) {
15+
const q = [root]
16+
while(q.length) {
17+
const size = q.length
18+
let target = false
19+
for(let i = 0; i < size; i++) {
20+
const tmp = q.shift()
21+
if(target) return tmp
22+
if(tmp === u) target = true
23+
if(tmp.left) q.push(tmp.left)
24+
if(tmp.right) q.push(tmp.right)
25+
}
26+
}
27+
return null
28+
};

0 commit comments

Comments
 (0)