Skip to content

Commit c916ba8

Browse files
committed
feat: ^_^
Signed-off-by: Luozhou (Siyuan Cao) <luozhou.csy@alibaba-inc.com>
1 parent 292d253 commit c916ba8

30 files changed

+2808
-0
lines changed

102.二叉树的层序遍历.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* @lc app=leetcode.cn id=102 lang=typescript
3+
*
4+
* [102] 二叉树的层序遍历
5+
*
6+
* https://leetcode.cn/problems/binary-tree-level-order-traversal/description/
7+
*
8+
* algorithms
9+
* Medium (65.02%)
10+
* Likes: 1426
11+
* Dislikes: 0
12+
* Total Accepted: 648.8K
13+
* Total Submissions: 997.8K
14+
* Testcase Example: '[3,9,20,null,null,15,7]'
15+
*
16+
* 给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
*
23+
* 输入:root = [3,9,20,null,null,15,7]
24+
* 输出:[[3],[9,20],[15,7]]
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
*
30+
* 输入:root = [1]
31+
* 输出:[[1]]
32+
*
33+
*
34+
* 示例 3:
35+
*
36+
*
37+
* 输入:root = []
38+
* 输出:[]
39+
*
40+
*
41+
*
42+
*
43+
* 提示:
44+
*
45+
*
46+
* 树中节点数目在范围 [0, 2000] 内
47+
* -1000 <= Node.val <= 1000
48+
*
49+
*
50+
*/
51+
52+
import { TreeNode } from './commons/Tree'
53+
54+
export
55+
// @lc code=start
56+
function levelOrder(root: TreeNode | null): number[][] {
57+
const result: number[][] = []
58+
const queue = [root].filter(Boolean)
59+
60+
while (queue.length) {
61+
const levelLength = queue.length
62+
const levelResult: number[] = []
63+
64+
for (let i = 0; i < levelLength; i++) {
65+
const node = queue.shift()!
66+
67+
if (node.left) {
68+
queue.push(node.left)
69+
}
70+
if (node.right) {
71+
queue.push(node.right)
72+
}
73+
74+
levelResult.push(node.val)
75+
}
76+
77+
result.push(levelResult)
78+
}
79+
80+
return result
81+
}
82+
// @lc code=end
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* @lc app=leetcode.cn id=103 lang=typescript
3+
*
4+
* [103] 二叉树的锯齿形层序遍历
5+
*
6+
* https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/description/
7+
*
8+
* algorithms
9+
* Medium (57.35%)
10+
* Likes: 681
11+
* Dislikes: 0
12+
* Total Accepted: 259.7K
13+
* Total Submissions: 452.8K
14+
* Testcase Example: '[3,9,20,null,null,15,7]'
15+
*
16+
* 给你二叉树的根节点 root ,返回其节点值的 锯齿形层序遍历 。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
*
23+
* 输入:root = [3,9,20,null,null,15,7]
24+
* 输出:[[3],[20,9],[15,7]]
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
*
30+
* 输入:root = [1]
31+
* 输出:[[1]]
32+
*
33+
*
34+
* 示例 3:
35+
*
36+
*
37+
* 输入:root = []
38+
* 输出:[]
39+
*
40+
*
41+
*
42+
*
43+
* 提示:
44+
*
45+
*
46+
* 树中节点数目在范围 [0, 2000] 内
47+
* -100 <= Node.val <= 100
48+
*
49+
*
50+
*/
51+
52+
import { TreeNode } from './commons/Tree'
53+
54+
export
55+
// @lc code=start
56+
function zigzagLevelOrder(root: TreeNode | null): number[][] {
57+
const queue = [root].filter(Boolean)
58+
const result: number[][] = []
59+
60+
while (queue.length) {
61+
const levelLength = queue.length
62+
const levelResult: number[] = []
63+
64+
for (let i = 0; i < levelLength; i++) {
65+
const node = queue.shift()!
66+
levelResult.push(node.val)
67+
68+
if (node.left) {
69+
queue.push(node.left)
70+
}
71+
if (node.right) {
72+
queue.push(node.right)
73+
}
74+
}
75+
76+
if (result.length & 1) {
77+
levelResult.reverse()
78+
}
79+
result.push(levelResult)
80+
}
81+
82+
return result
83+
}
84+
// @lc code=end
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* @lc app=leetcode.cn id=116 lang=typescript
3+
*
4+
* [116] 填充每个节点的下一个右侧节点指针
5+
*
6+
* https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/description/
7+
*
8+
* algorithms
9+
* Medium (71.92%)
10+
* Likes: 850
11+
* Dislikes: 0
12+
* Total Accepted: 282.9K
13+
* Total Submissions: 393.4K
14+
* Testcase Example: '[1,2,3,4,5,6,7]'
15+
*
16+
* 给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
17+
*
18+
*
19+
* struct Node {
20+
* ⁠ int val;
21+
* ⁠ Node *left;
22+
* ⁠ Node *right;
23+
* ⁠ Node *next;
24+
* }
25+
*
26+
* 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
27+
*
28+
* 初始状态下,所有 next 指针都被设置为 NULL。
29+
*
30+
*
31+
*
32+
* 示例 1:
33+
*
34+
*
35+
*
36+
*
37+
* 输入:root = [1,2,3,4,5,6,7]
38+
* 输出:[1,#,2,3,#,4,5,6,7,#]
39+
* 解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B
40+
* 所示。序列化的输出按层序遍历排列,同一层节点由 next 指针连接,'#' 标志着每一层的结束。
41+
*
42+
*
43+
*
44+
*
45+
* 示例 2:
46+
*
47+
*
48+
* 输入:root = []
49+
* 输出:[]
50+
*
51+
*
52+
*
53+
*
54+
* 提示:
55+
*
56+
*
57+
* 树中节点的数量在 [0, 2^12 - 1] 范围内
58+
* -1000 <= node.val <= 1000
59+
*
60+
*
61+
*
62+
*
63+
* 进阶:
64+
*
65+
*
66+
* 你只能使用常量级额外空间。
67+
* 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
68+
*
69+
*
70+
*/
71+
72+
export
73+
// @lc code=start
74+
function connect(root: Node | null): Node | null {
75+
let currLevel = root
76+
while (currLevel?.left) {
77+
let parent: Node | null = currLevel
78+
let prevChild: Node | null = null
79+
const nextLevel = currLevel.left
80+
81+
while (parent) {
82+
if (prevChild) prevChild.next = parent.left
83+
parent.left!.next = parent.right
84+
prevChild = parent.right
85+
86+
parent = parent.next
87+
}
88+
89+
currLevel = nextLevel
90+
}
91+
92+
return root
93+
}
94+
// @lc code=end
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* @lc app=leetcode.cn id=117 lang=typescript
3+
*
4+
* [117] 填充每个节点的下一个右侧节点指针 II
5+
*
6+
* https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/description/
7+
*
8+
* algorithms
9+
* Medium (64.58%)
10+
* Likes: 621
11+
* Dislikes: 0
12+
* Total Accepted: 150.6K
13+
* Total Submissions: 233.2K
14+
* Testcase Example: '[1,2,3,4,5,null,7]'
15+
*
16+
* 给定一个二叉树
17+
*
18+
*
19+
* struct Node {
20+
* ⁠ int val;
21+
* ⁠ Node *left;
22+
* ⁠ Node *right;
23+
* ⁠ Node *next;
24+
* }
25+
*
26+
* 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
27+
*
28+
* 初始状态下,所有 next 指针都被设置为 NULL。
29+
*
30+
*
31+
*
32+
* 进阶:
33+
*
34+
*
35+
* 你只能使用常量级额外空间。
36+
* 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
37+
*
38+
*
39+
*
40+
*
41+
* 示例:
42+
*
43+
*
44+
*
45+
*
46+
* 输入:root = [1,2,3,4,5,null,7]
47+
* 输出:[1,#,2,3,#,4,5,7,#]
48+
* 解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化输出按层序遍历顺序(由 next
49+
* 指针连接),'#' 表示每层的末尾。
50+
*
51+
*
52+
*
53+
* 提示:
54+
*
55+
*
56+
* 树中的节点数小于 6000
57+
* -100
58+
*
59+
*
60+
*
61+
*
62+
*
63+
*
64+
*
65+
*/
66+
67+
export
68+
// @lc code=start
69+
function connect(root: Node | null): Node | null {
70+
let currLevelFirstNode = root
71+
72+
while (currLevelFirstNode) {
73+
let curr: Node | null = currLevelFirstNode
74+
let prevChild: Node | null = null
75+
let nextLevelFirstNode: Node | null = null
76+
77+
while (curr) {
78+
nextLevelFirstNode = nextLevelFirstNode || curr.left || curr.right
79+
prevChild = connectTwo(prevChild, curr.left)
80+
prevChild = connectTwo(prevChild, curr.right)
81+
82+
curr = curr.next
83+
}
84+
85+
currLevelFirstNode = nextLevelFirstNode
86+
}
87+
88+
return root
89+
}
90+
91+
function connectTwo(prev: Node | null, curr: Node | null) {
92+
if (!curr) {
93+
return prev
94+
}
95+
if (!prev) {
96+
return curr
97+
}
98+
prev.next = curr
99+
return curr
100+
}
101+
// @lc code=end

0 commit comments

Comments
 (0)