8000 feat(45): 递归解法 · descire/LeetCode@156b617 · GitHub
[go: up one dir, main page]

Skip to content

Commit 156b617

Browse files
committed
feat(45): 递归解法
1 parent 0817134 commit 156b617

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Binary-Tree/45/solution1.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 时间复杂度:O(n)
3+
* 空间复杂度:O(n)
4+
*/
5+
const findBottomLeftValue = (root) => {
6+
let ans = [];
7+
dfs(root, 0, ans);
8+
return ans.pop();
9+
}
10+
11+
function dfs(root, level, ans) {
12+
if (!root) {
13+
return;
14+
}
15+
16+
if (ans[level] === undefined) {
17+
ans[level] = root.val;
18+
}
19+
20+
dfs(root.left, level + 1, ans);
21+
dfs(root.right, level + 1, ans);
22+
}

0 commit comments

Comments
 (0)
0