Skip to content

Commit ae738f1

Browse files
authored
Added tasks 452-918
1 parent 9a21aa2 commit ae738f1

File tree

19 files changed

+622
-0
lines changed

19 files changed

+622
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
236236
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
237237
|-|-|-|-|-|-
238238
| 0053 |[Maximum Subarray](src/main/ts/g0001_0100/s0053_maximum_subarray/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
239+
| 0918 |[Maximum Sum Circular Subarray](src/main/ts/g0901_1000/s0918_maximum_sum_circular_subarray/solution.ts)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue, Medium, Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue | 2 | 91.04
239240

240241
#### Day 6
241242

@@ -1111,6 +1112,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11111112
| 0228 |[Summary Ranges](src/main/ts/g0201_0300/s0228_summary_ranges/solution.ts)| Easy | Array | 0 | 100.00
11121113
| 0056 |[Merge Intervals](src/main/ts/g0001_0100/s0056_merge_intervals/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 7 | 87.99
11131114
| 0057 |[Insert Interval](src/main/ts/g0001_0100/s0057_insert_interval/solution.ts)| Medium | Array | 0 | 100.00
1115+
| 0452 |[Minimum Number of Arrows to Burst Balloons](src/main/ts/g0401_0500/s0452_minimum_number_of_arrows_to_burst_balloons/solution.ts)| Medium | Array, Sorting, Greedy | 75 | 98.54
11141116

11151117
#### Top Interview 150 Stack
11161118

@@ -1161,13 +1163,15 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11611163
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11621164
|-|-|-|-|-|-
11631165
| 0199 |[Binary Tree Right Side View](src/main/ts/g0101_0200/s0199_binary_tree_right_side_view/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
1166+
| 0637 |[Average of Levels in Binary Tree](src/main/ts/g0601_0700/s0637_average_of_levels_in_binary_tree/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
11641167
| 0102 |[Binary Tree Level Order Traversal](src/main/ts/g0101_0200/s0102_binary_tree_level_order_traversal/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
11651168
| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/ts/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal/solution.ts)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
11661169

11671170
#### Top Interview 150 Binary Search Tree
11681171

11691172
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11701173
|-|-|-|-|-|-
1174+
| 0530 |[Minimum Absolute Difference in BST](src/main/ts/g0501_0600/s0530_minimum_absolute_difference_in_bst/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 0 | 100.00
11711175
| 0230 |[Kth Smallest Element in a BST](src/main/ts/g0201_0300/s0230_kth_smallest_element_in_a_bst/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
11721176
| 0098 |[Validate Binary Search Tree](src/main/ts/g0001_0100/s0098_validate_binary_search_tree/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00
11731177

@@ -1183,6 +1187,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11831187

11841188
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11851189
|-|-|-|-|-|-
1190+
| 0909 |[Snakes and Ladders](src/main/ts/g0901_1000/s0909_snakes_and_ladders/solution.ts)| Medium | Array, Breadth_First_Search, Matrix | 5 | 98.27
11861191
| 0433 |[Minimum Genetic Mutation](src/main/ts/g0401_0500/s0433_minimum_genetic_mutation/solution.ts)| Medium | String, Hash_Table, Breadth_First_Search | 0 | 100.00
11871192
| 0127 |[Word Ladder](src/main/ts/g0101_0200/s0127_word_ladder/solution.ts)| Hard | Top_Interview_Questions, String, Hash_Table, Breadth_First_Search | 41 | 95.63
11881193

@@ -1220,6 +1225,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
12201225
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12211226
|-|-|-|-|-|-
12221227
| 0053 |[Maximum Subarray](src/main/ts/g0001_0100/s0053_maximum_subarray/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1228+
| 0918 |[Maximum Sum Circular Subarray](src/main/ts/g0901_1000/s0918_maximum_sum_circular_subarray/solution.ts)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue, Medium, Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue | 2 | 91.04
12231229

12241230
#### Top Interview 150 Binary Search
12251231

@@ -1238,6 +1244,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
12381244
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12391245
|-|-|-|-|-|-
12401246
| 0215 |[Kth Largest Element in an Array](src/main/ts/g0201_0300/s0215_kth_largest_element_in_an_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 4 | 99.64
1247+
| 0502 |[IPO](src/main/ts/g0501_0600/s0502_ipo/solution.ts)| Hard | Array, Sorting, Greedy, Heap_Priority_Queue | 193 | 89.19
12411248
| 0373 |[Find K Pairs with Smallest Sums](src/main/ts/g0301_0400/s0373_find_k_pairs_with_smallest_sums/solution.ts)| Medium | Array, Heap_Priority_Queue | 42 | 85.15
12421249
| 0295 |[Find Median from Data Stream](src/main/ts/g0201_0300/s0295_find_median_from_data_stream/solution.ts)| Hard | Top_100_Liked_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Big_O_Time_O(n\*log_n)_Space_O(n) | 106 | 92.31
12431250

@@ -1743,12 +1750,18 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
17431750
| # | Title | Difficulty | Tag | Time, ms | Time, %
17441751
|------|----------------|-------------|-------------|----------|---------
17451752
| 1143 |[Longest Common Subsequence](src/main/ts/g1101_1200/s1143_longest_common_subsequence/solution.ts)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 50 | 69.40
1753+
| 0918 |[Maximum Sum Circular Subarray](src/main/ts/g0901_1000/s0918_maximum_sum_circular_subarray/solution.ts)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue, Medium, Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue, Dynamic_Programming_I_Day_5, Top_Interview_150_Kadane's_Algorithm | 2 | 91.04
1754+
| 0909 |[Snakes and Ladders](src/main/ts/g0901_1000/s0909_snakes_and_ladders/solution.ts)| Medium | Array, Breadth_First_Search, Matrix, Top_Interview_150_Graph_BFS | 5 | 98.27
17461755
| 0763 |[Partition Labels](src/main/ts/g0701_0800/s0763_partition_labels/solution.ts)| Medium | String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String, Big_O_Time_O(n)_Space_O(1) | 4 | 86.89
17471756
| 0739 |[Daily Temperatures](src/main/ts/g0701_0800/s0739_daily_temperatures/solution.ts)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6, Big_O_Time_O(n)_Space_O(n) | 18 | 80.57
17481757
| 0647 |[Palindromic Substrings](src/main/ts/g0601_0700/s0647_palindromic_substrings/solution.ts)| Medium | String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n) | 5 | 100.00
1758+
| 0637 |[Average of Levels in Binary Tree](src/main/ts/g0601_0700/s0637_average_of_levels_in_binary_tree/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Top_Interview_150_Binary_Tree_BFS | 0 | 100.00
17491759
| 0560 |[Subarray Sum Equals K](src/main/ts/g0501_0600/s0560_subarray_sum_equals_k/solution.ts)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array, Big_O_Time_O(n)_Space_O(n) | 14 | 87.34
17501760
| 0543 |[Diameter of Binary Tree](src/main/ts/g0501_0600/s0543_diameter_of_binary_tree/solution.ts)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 1 | 87.16
1761+
| 0530 |[Minimum Absolute Difference in BST](src/main/ts/g0501_0600/s0530_minimum_absolute_difference_in_bst/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Top_Interview_150_Binary_Search_Tree | 0 | 100.00
1762+
| 0502 |[IPO](src/main/ts/g0501_0600/s0502_ipo/solution.ts)| Hard | Array, Sorting, Greedy, Heap_Priority_Queue, Top_Interview_150_Heap | 193 | 89.19
17511763
| 0494 |[Target Sum](src/main/ts/g0401_0500/s0494_target_sum/solution.ts)| Medium | Array, Dynamic_Programming, Backtracking, Big_O_Time_O(n\*(sum+s))_Space_O(n\*(sum+s)) | 24 | 83.43
1764+
| 0452 |[Minimum Number of Arrows to Burst Balloons](src/main/ts/g0401_0500/s0452_minimum_number_of_arrows_to_burst_balloons/solution.ts)| Medium | Array, Sorting, Greedy, Top_Interview_150_Intervals | 75 | 98.54
17521765
| 0438 |[Find All Anagrams in a String](src/main/ts/g0401_0500/s0438_find_all_anagrams_in_a_string/solution.ts)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer, Big_O_Time_O(n+m)_Space_O(1) | 8 | 97.80
17531766
| 0437 |[Path Sum III](src/main/ts/g0401_0500/s0437_path_sum_iii/solution.ts)| Medium | Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Big_O_Time_O(n)_Space_O(n) | 3 | 86.41
17541767
| 0433 |[Minimum Genetic Mutation](src/main/ts/g0401_0500/s0433_minimum_genetic_mutation/solution.ts)| Medium | String, Hash_Table, Breadth_First_Search, Graph_Theory_I_Day_12_Breadth_First_Search, Top_Interview_150_Graph_BFS | 0 | 100.00
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
452\. Minimum Number of Arrows to Burst Balloons
2+
3+
Medium
4+
5+
There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array `points` where <code>points[i] = [x<sub>start</sub>, x<sub>end</sub>]</code> denotes a balloon whose **horizontal diameter** stretches between <code>x<sub>start</sub></code> and <code>x<sub>end</sub></code>. You do not know the exact y-coordinates of the balloons.
6+
7+
Arrows can be shot up **directly vertically** (in the positive y-direction) from different points along the x-axis. A balloon with <code>x<sub>start</sub></code> and <code>x<sub>end</sub></code> is **burst** by an arrow shot at `x` if <code>x<sub>start</sub> <= x <= x<sub>end</sub></code>. There is **no limit** to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.
8+
9+
Given the array `points`, return _the **minimum** number of arrows that must be shot to burst all balloons_.
10+
11+
**Example 1:**
12+
13+
**Input:** points = [[10,16],[2,8],[1,6],[7,12]]
14+
15+
**Output:** 2
16+
17+
**Explanation:** The balloons can be burst by 2 arrows:
18+
19+
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
20+
21+
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
22+
23+
**Example 2:**
24+
25+
**Input:** points = [[1,2],[3,4],[5,6],[7,8]]
26+
27+
**Output:** 4
28+
29+
**Explanation:** One arrow needs to be shot for each balloon for a total of 4 arrows.
30+
31+
**Example 3:**
32+
33+
**Input:** points = [[1,2],[2,3],[3,4],[4,5]]
34+
35+
**Output:** 2
36+
37+
**Explanation:** The balloons can be burst by 2 arrows:
38+
39+
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
40+
41+
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
42+
43+
**Constraints:**
44+
45+
* <code>1 <= points.length <= 10<sup>5</sup></code>
46+
* `points[i].length == 2`
47+
* <code>-2<sup>31</sup> <= x<sub>start</sub> < x<sub>end</sub> <= 2<sup>31</sup> - 1</code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// #Medium #Array #Sorting #Greedy #Top_Interview_150_Intervals
2+
// #2025_04_15_Time_75_ms_(98.54%)_Space_79.44_MB_(62.20%)
3+
4+
function findMinArrowShots(points: number[][]): number {
5+
if (points.length === 0) {
6+
return 0
7+
}
8+
points.sort((a, b) => a[1] - b[1])
9+
let minArrows = 1
10+
let end = points[0][1]
11+
for (let i = 1; i < points.length; i++) {
12+
if (points[i][0] > end) {
13+
minArrows++
14+
end = points[i][1]
15+
}
16+
}
17+
return minArrows
18+
}
19+
20+
export { findMinArrowShots }
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
502\. IPO
2+
3+
Hard
4+
5+
Suppose LeetCode will start its **IPO** soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the **IPO**. Since it has limited resources, it can only finish at most `k` distinct projects before the **IPO**. Help LeetCode design the best way to maximize its total capital after finishing at most `k` distinct projects.
6+
7+
You are given `n` projects where the <code>i<sup>th</sup></code> project has a pure profit `profits[i]` and a minimum capital of `capital[i]` is needed to start it.
8+
9+
Initially, you have `w` capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.
10+
11+
Pick a list of **at most** `k` distinct projects from given projects to **maximize your final capital**, and return _the final maximized capital_.
12+
13+
The answer is guaranteed to fit in a 32-bit signed integer.
14+
15+
**Example 1:**
16+
17+
**Input:** k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
18+
19+
**Output:** 4
20+
21+
**Explanation:**
22+
23+
Since your initial capital is 0, you can only start the project indexed 0.
24+
25+
After finishing it you will obtain profit 1 and your capital becomes 1.
26+
27+
With capital 1, you can either start the project indexed 1 or the project indexed 2.
28+
29+
Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
30+
31+
Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.
32+
33+
**Example 2:**
34+
35+
**Input:** k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
36+
37+
**Output:** 6
38+
39+
**Constraints:**
40+
41+
* <code>1 <= k <= 10<sup>5</sup></code>
42+
* <code>0 <= w <= 10<sup>9</sup></code>
43+
* `n == profits.length`
44+
* `n == capital.length`
45+
* <code>1 <= n <= 10<sup>5</sup></code>
46+
* <code>0 <= profits[i] <= 10<sup>4</sup></code>
47+
* <code>0 <= capital[i] <= 10<sup>9</sup></code>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// #Hard #Array #Sorting #Greedy #Heap_Priority_Queue #Top_Interview_150_Heap
2+
// #2025_04_15_Time_193_ms_(89.19%)_Space_102.47_MB_(8.11%)
3+
4+
class MaxHeap {
5+
private heap: number[];
6+
7+
constructor() {
8+
this.heap = [];
9+
}
10+
11+
push(value: number) {
12+
this.heap.push(value);
13+
this.heapifyUp();
14+
}
15+
16+
pop(): number | undefined {
17+
if (this.heap.length === 0) {
18+
return undefined;
19+
}
20+
if (this.heap.length === 1) {
21+
return this.heap.pop();
22+
}
23+
const max = this.heap[0];
24+
this.heap[0] = this.heap.pop()!;
25+
this.heapifyDown();
26+
return max;
27+
}
28+
29+
isEmpty(): boolean {
30+
return this.heap.length === 0;
31+
}
32+
33+
private heapifyUp() {
34+
let index = this.heap.length - 1;
35+
while (index > 0) {
36+
let parentIndex = Math.floor((index - 1) / 2);
37+
if (this.heap[parentIndex] >= this.heap[index]) {
38+
break;
39+
}
40+
[this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]];
41+
index = parentIndex;
42+
}
43+
}
44+
45+
private heapifyDown() {
46+
let index = 0;
47+
while (true) {
48+
let left = 2 * index + 1;
49+
let right = 2 * index + 2;
50+
let largest = index;
51+
if (left < this.heap.length && this.heap[left] > this.heap[largest]) {
52+
largest = left;
53+
}
54+
if (right < this.heap.length && this.heap[right] > this.heap[largest]) {
55+
largest = right;
56+
}
57+
if (largest === index) break;
58+
[this.heap[index], this.heap[largest]] = [this.heap[largest], this.heap[index]];
59+
index = largest;
60+
}
61+
}
62+
}
63+
64+
function findMaximizedCapital(k: number, w: number, profits: number[], capital: number[]): number {
65+
let projects: [number, number][] = [];
66+
const n = profits.length;
67+
for (let i = 0; i < n; i++) {
68+
projects.push([capital[i], profits[i]]);
69+
}
70+
projects.sort((a, b) => a[0] - b[0]);
71+
const maxHeap = new MaxHeap();
72+
let i = 0;
73+
while (k--) {
74+
while (i < n && projects[i][0] <= w) {
75+
maxHeap.push(projects[i][1]);
76+
i++;
77+
}
78+
if (maxHeap.isEmpty()) {
79+
break;
80+
}
81+
w += maxHeap.pop()!;
82+
}
83+
return w;
84+
}
85+
86+
export { findMaximizedCapital }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
530\. Minimum Absolute Difference in BST
2+
3+
Easy
4+
5+
Given the `root` of a Binary Search Tree (BST), return _the minimum absolute difference between the values of any two different nodes in the tree_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg)
10+
11+
**Input:** root = [4,2,6,1,3]
12+
13+
**Output:** 1
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg)
18+
19+
**Input:** root = [1,0,48,null,null,12,49]
20+
21+
**Output:** 1
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.
26+
* <code>0 <= Node.val <= 10<sup>5</sup></code>
27+
28+
**Note:** This question is the same as 783: [https://leetcode.com/problems/minimum-distance-between-bst-nodes/](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
2+
// #Top_Interview_150_Binary_Search_Tree #2025_04_15_Time_0_ms_(100.00%)_Space_61.15_MB_(83.13%)
3+
4+
import { TreeNode } from '../../com_github_leetcode/treenode'
5+
6+
/**
7+
* Definition for a binary tree node.
8+
* class TreeNode {
9+
* val: number
10+
* left: TreeNode | null
11+
* right: TreeNode | null
12+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
13+
* this.val = (val===undefined ? 0 : val)
14+
* this.left = (left===undefined ? null : left)
15+
* this.right = (right===undefined ? null : right)
16+
* }
17+
* }
18+
*/
19+
function getMinimumDifference(root: TreeNode | null): number {
20+
let ans = Number.MAX_SAFE_INTEGER
21+
let prev: number | null = null
22+
function dfs(node: TreeNode | null) {
23+
if (!node) {
24+
return
25+
}
26+
dfs(node.left)
27+
if (prev !== null) {
28+
ans = Math.min(ans, Math.abs(node.val - prev))
29+
}
30+
prev = node.val
31+
dfs(node.right)
32+
}
33+
dfs(root)
34+
return ans
35+
}
36+
37+
export { getMinimumDifference }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
637\. Average of Levels in Binary Tree
2+
3+
Easy
4+
5+
Given the `root` of a binary tree, return _the average value of the nodes on each level in the form of an array_. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/03/09/avg1-tree.jpg)
10+
11+
**Input:** root = [3,9,20,null,null,15,7]
12+
13+
**Output:** [3.00000,14.50000,11.00000] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/03/09/avg2-tree.jpg)
18+
19+
**Input:** root = [3,9,20,15,7]
20+
21+
**Output:** [3.00000,14.50000,11.00000]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
26+
* <code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code>

0 commit comments

Comments
 (0)