-
Notifications
You must be signed in to change notification settings - Fork 0
/
513-Find_Bottom_Left_Tree_Value.html
57 lines (45 loc) · 1.13 KB
/
513-Find_Bottom_Left_Tree_Value.html
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
<html>
<script>
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var findBottomLeftValue = function (root) {
let h = 0;
let ret = root.val;
let findBottom = function (node, level) {
if (level > h) {
h = level;
ret = node.val;
}
if (node.left) {
findBottom(node.left, level + 1);
}
if (node.right) {
findBottom(node.right, level + 1);
}
}
findBottom(root, 0);
return ret;
};
var findBottomLeftValue2 = function (root) {
let array = [];
let ret;
array.push(root);
while (array.length > 0) {
let node = array.shift();
ret = node.val;
node.right ? array.push(node.right) : null;
node.left ? array.push(node.left) : null;
}
return ret;
};
</script>
</html>