-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path0_level-order-successor.js
63 lines (59 loc) · 1.48 KB
/
0_level-order-successor.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const { buildTreeBFS } = require('../_utils');
/**
*
* Problem:
* Given a binary tree and a node, find the level order successor of the given node
* in the tree. The level order successor is the node that appears right after the
* given node in the level order traversal.
*
*
* Example 1:
* Input: [1, 2, 3, 4, 5], 3
* Output: 4
*
* Example 2:
* Input: [12, 7, 1, null, 9, 10, 5], 9
* Output: 10
*
*
* Time: O(n)
* Space: O(n)
*
* @param {TreeNode} root
* @param {number} nodeValue
* @return {TreeNode}
*/
function levelOrderSuccessor(root, nodeValue) {
if (!root) {
return null;
}
const queue = [root];
while (queue.length > 0) {
const node = queue.shift();
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
/**
* Follow the traditional BFS traversal process, if the value is found, break
* the loop. Note here we always push the children nodes to the queue first
* before breaking, so when the value is found, its successor should already
* in the queue (unless it's the last node), the first node in the queue is
* what we want.
*/
if (node.val === nodeValue) {
break;
}
}
if (queue.length > 0) {
return queue[0];
}
return null;
}
// Test
const tree1 = buildTreeBFS([1, 2, 3, 4, 5]);
console.log(levelOrderSuccessor(tree1, 3).val); // 4
const tree2 = buildTreeBFS([12, 7, 1, null, 9, 10, 5]);
console.log(levelOrderSuccessor(tree2, 9).val); // 10