-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path513_FindBottomLeftTreeValue.js
60 lines (55 loc) · 1.47 KB
/
513_FindBottomLeftTreeValue.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
// https://leetcode.com/problems/find-bottom-left-tree-value/#/description
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
/**
* @param {TreeNode} root
* @return {number}
*/
var findBottomLeftValue = function (root) {
var obj = {};
var treeToObj = function (node, deepth) {
if (!obj[deepth]) obj[deepth] = [];
obj[deepth].push(node.val);
if (node.left) {
treeToObj(node.left, deepth + 1);
} else {
obj[deepth + 1] = obj[deepth + 1] || [];
obj[deepth + 1].push(null);
}
if (node.right) {
treeToObj(node.right, deepth + 1);
} else {
obj[deepth + 1] = obj[deepth + 1] || [];
obj[deepth + 1].push(null);
}
}
treeToObj(root, 0);
var keys = Object.keys(obj);
var bottomRow = obj[keys.length - 2];
for (var i = 0; i < bottomRow.length; i++) {
if (bottomRow[i] === null) continue;
return bottomRow[i];
}
};
var root = new TreeNode(2);
root.left = new TreeNode(1);
root.right = new TreeNode(3);
console.log(findBottomLeftValue(root));
root = new TreeNode(1);
root.left = new TreeNode(2);
root.left.left = new TreeNode(4);
root.right = new TreeNode(3);
root.right.left = new TreeNode(5);
root.right.right = new TreeNode(6);
root.right.left.left = new TreeNode(7);
root.right.left.left.right = new TreeNode(8);
console.log(findBottomLeftValue(root));