-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem80.js
43 lines (40 loc) · 1.01 KB
/
Problem80.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
// Problem 80
//
// This problem was asked by Google.
//
// Given the root of a binary tree, return a deepest node. For example, in the following tree, return d.
//
// a
// / \
// b c
// /
// d
//
// O(N) Time complexity
// O(N) Space complexity
// N is the number of nodes in the tree
/**
* Returns the deepest node
* @param {TreeNode} root
* @return {TreeNode}
*/
function deepestNode(root) {
// can do any type of tree traversal. Just keep count of the level.
// At each node, if the node's level is greater than the current level, update the return node and update the level
if (root === null) return null;
// Level order traversal
const queue = [];
queue.push(root);
let prev = null;
while (queue.length !== 0) {
const size = queue.length;
for (let i = 0; i < size; i++) {
const node = queue.shift();
prev = node;
if (node.left !== null) queue.push(node.left);
if (node.right !== null) queue.push(node.right);
}
}
return prev;
}
export default deepestNode;