Skip to content

Commit f5bbfeb

Browse files
committed
Solve LeetCode 75 binary tree BFS problems
1 parent 10165a7 commit f5bbfeb

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

LeetCode-75/39-199.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// https://leetcode.com/problems/binary-tree-right-side-view/description/?envType=study-plan-v2&envId=leetcode-75
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* val: number
7+
* left: TreeNode | null
8+
* right: TreeNode | null
9+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.left = (left===undefined ? null : left)
12+
* this.right = (right===undefined ? null : right)
13+
* }
14+
* }
15+
*/
16+
17+
// @ts-nocheck
18+
const rightSideView = (root: TreeNode | null): number[] => {
19+
if (root === null) return [];
20+
21+
const values = [];
22+
const queue = [root];
23+
24+
while (queue.length) {
25+
const initialQueueLength = queue.length;
26+
27+
for (let i = 0; i < initialQueueLength; i++) {
28+
const current = queue.shift();
29+
if (i === initialQueueLength - 1) values.push(current.val);
30+
if (current.left) queue.push(current.left);
31+
if (current.right) queue.push(current.right);
32+
}
33+
}
34+
35+
return values;
36+
};

LeetCode-75/40-1161.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/description/?envType=study-plan-v2&envId=leetcode-75
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* val: number
7+
* left: TreeNode | null
8+
* right: TreeNode | null
9+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.left = (left===undefined ? null : left)
12+
* this.right = (right===undefined ? null : right)
13+
* }
14+
* }
15+
*/
16+
17+
// @ts-nocheck
18+
const maxLevelSum = (root: TreeNode | null): number => {
19+
const queue = [root];
20+
21+
let level = 1,
22+
maxSum = root.val,
23+
maxLevel = 1;
24+
25+
while (queue.length) {
26+
const initialQueueLength = queue.length;
27+
let tempSum = 0;
28+
29+
for (let i = 0; i < initialQueueLength; i++) {
30+
const current = queue.shift();
31+
tempSum += current.val;
32+
if (current.left) queue.push(current.left);
33+
if (current.right) queue.push(current.right);
34+
}
35+
36+
if (tempSum > maxSum) {
37+
maxLevel = level;
38+
maxSum = tempSum;
39+
}
40+
41+
level++;
42+
}
43+
44+
return maxLevel;
45+
};

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969

7070
### Binary Tree - BFS
7171

72-
- [ ] 39. Binary Tree Right Side View
73-
- [ ] 40. Maximum Level Sum of a Binary Tree
72+
- [X] 39. Binary Tree Right Side View
73+
- [X] 40. Maximum Level Sum of a Binary Tree
7474

7575
### Binary Search Tree
7676

0 commit comments

Comments
 (0)