-
Notifications
You must be signed in to change notification settings - Fork 0
/
513.找树左下角的值.ts
47 lines (37 loc) · 944 Bytes
/
513.找树左下角的值.ts
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
/*
* @lc app=leetcode.cn id=513 lang=typescript
*
* [513] 找树左下角的值
*/
// @lc code=start
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function findBottomLeftValue(root: TreeNode | null): number {
const queue = [root]
let res: number = 0
while (queue.length) {
let len = queue.length
for (let i = 0; i < len; i++) {
// 出队
const node = queue.shift()
if (i === 0) {
res = node.val
}
node.left && queue.push(node.left)
node.right && queue.push(node.right)
}
}
return res
};
// @lc code=end