Skip to content

Commit e0ff6de

Browse files
authored
Added tasks 399, 530, 637
1 parent 84be1ce commit e0ff6de

File tree

7 files changed

+266
-0
lines changed

7 files changed

+266
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace LeetCodeNet.G0301_0400.S0399_evaluate_division {
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Graph #Union_Find #Shortest_Path
4+
// #LeetCode_75_Graphs/DFS #Top_Interview_150_Graph_General
5+
// #2025_07_18_Time_3_ms_(66.56%)_Space_47.65_MB_(86.96%)
6+
7+
using System;
8+
using System.Collections.Generic;
9+
10+
public class Solution {
11+
private Dictionary<string, string> root;
12+
private Dictionary<string, double> rate;
13+
14+
public double[] CalcEquation(
15+
IList<IList<string>> equations, double[] values, IList<IList<string>> queries) {
16+
root = new Dictionary<string, string>();
17+
rate = new Dictionary<string, double>();
18+
int n = equations.Count;
19+
foreach (var equation in equations) {
20+
string x = equation[0];
21+
string y = equation[1];
22+
root[x] = x;
23+
root[y] = y;
24+
rate[x] = 1.0;
25+
rate[y] = 1.0;
26+
}
27+
for (int i = 0; i < n; ++i) {
28+
string x = equations[i][0];
29+
string y = equations[i][1];
30+
union(x, y, values[i]);
31+
}
32+
double[] result = new double[queries.Count];
33+
for (int i = 0; i < queries.Count; ++i) {
34+
string x = queries[i][0];
35+
string y = queries[i][1];
36+
if (!root.ContainsKey(x) || !root.ContainsKey(y)) {
37+
result[i] = -1;
38+
continue;
39+
}
40+
string rootX = findRoot(x, x, 1.0);
41+
string rootY = findRoot(y, y, 1.0);
42+
result[i] = rootX.Equals(rootY) ? rate[x] / rate[y] : -1.0;
43+
}
44+
return result;
45+
}
46+
47+
private void union(string x, string y, double v) {
48+
string rootX = findRoot(x, x, 1.0);
49+
string rootY = findRoot(y, y, 1.0);
50+
root[rootX] = rootY;
51+
double r1 = rate[x];
52+
double r2 = rate[y];
53+
rate[rootX] = v * r2 / r1;
54+
}
55+
56+
private string findRoot(string originalX, string x, double r) {
57+
if (root[x].Equals(x)) {
58+
root[originalX] = x;
59+
rate[originalX] = r * rate[x];
60+
return x;
61+
}
62+
return findRoot(originalX, root[x], r * rate[x]);
63+
}
64+
}
65+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
399\. Evaluate Division
2+
3+
Medium
4+
5+
You are given an array of variable pairs `equations` and an array of real numbers `values`, where <code>equations[i] = [A<sub>i</sub>, B<sub>i</sub>]</code> and `values[i]` represent the equation <code>A<sub>i</sub> / B<sub>i</sub> = values[i]</code>. Each <code>A<sub>i</sub></code> or <code>B<sub>i</sub></code> is a string that represents a single variable.
6+
7+
You are also given some `queries`, where <code>queries[j] = [C<sub>j</sub>, D<sub>j</sub>]</code> represents the <code>j<sup>th</sup></code> query where you must find the answer for <code>C<sub>j</sub> / D<sub>j</sub> = ?</code>.
8+
9+
Return _the answers to all queries_. If a single answer cannot be determined, return `-1.0`.
10+
11+
**Note:** The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.
12+
13+
**Example 1:**
14+
15+
**Input:** equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
16+
17+
**Output:** [6.00000,0.50000,-1.00000,1.00000,-1.00000]
18+
19+
**Explanation:**
20+
21+
Given: _a / b = 2.0_, _b / c = 3.0_
22+
23+
queries are: _a / c = ?_, _b / a = ?_, _a / e = ?_, _a / a = ?_, _x / x = ?_
24+
25+
return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
26+
27+
**Example 2:**
28+
29+
**Input:** equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
30+
31+
**Output:** [3.75000,0.40000,5.00000,0.20000]
32+
33+
**Example 3:**
34+
35+
**Input:** equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
36+
37+
**Output:** [0.50000,2.00000,-1.00000,-1.00000]
38+
39+
**Constraints:**
40+
41+
* `1 <= equations.length <= 20`
42+
* `equations[i].length == 2`
43+
* <code>1 <= A<sub>i</sub>.length, B<sub>i</sub>.length <= 5</code>
44+
* `values.length == equations.length`
45+
* `0.0 < values[i] <= 20.0`
46+
* `1 <= queries.length <= 20`
47+
* `queries[i].length == 2`
48+
* <code>1 <= C<sub>j</sub>.length, D<sub>j</sub>.length <= 5</code>
49+
* <code>A<sub>i</sub>, B<sub>i</sub>, C<sub>j</sub>, D<sub>j</sub></code> consist of lower case English letters and digits.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace LeetCodeNet.G0501_0600.S0530_minimum_absolute_difference_in_bst {
2+
3+
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
4+
// #Top_Interview_150_Binary_Search_Tree #2025_07_18_Time_0_ms_(100.00%)_Space_46.22_MB_(60.28%)
5+
6+
using System;
7+
using LeetCodeNet.Com_github_leetcode;
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* public int val;
13+
* public TreeNode left;
14+
* public TreeNode right;
15+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
16+
* this.val = val;
17+
* this.left = left;
18+
* this.right = right;
19+
* }
20+
* }
21+
*/
22+
public class Solution {
23+
private int ans = int.MaxValue;
24+
private int? prev = null;
25+
26+
public int GetMinimumDifference(TreeNode root) {
27+
InOrder(root);
28+
return ans;
29+
}
30+
31+
private void InOrder(TreeNode node) {
32+
if (node == null) {
33+
return;
34+
}
35+
InOrder(node.left);
36+
if (prev != null) {
37+
ans = Math.Min(ans, Math.Abs((int) node.val - prev.Value));
38+
}
39+
prev = node.val;
40+
InOrder(node.right);
41+
}
42+
}
43+
}
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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace LeetCodeNet.G0601_0700.S0637_average_of_levels_in_binary_tree {
2+
3+
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
4+
// #Top_Interview_150_Binary_Tree_BFS #2025_07_18_Time_1_ms_(100.00%)_Space_50.69_MB_(89.38%)
5+
6+
using System;
7+
using System.Collections.Generic;
8+
using LeetCodeNet.Com_github_leetcode;
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* public class TreeNode {
13+
* public int val;
14+
* public TreeNode left;
15+
* public TreeNode right;
16+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
public class Solution {
24+
public IList<double> AverageOfLevels(TreeNode root) {
25+
IList<int> count = new List<int>();
26+
IList<double> avg = new List<double>();
27+
Average(root, 0, count, avg);
28+
for(int i = 0; i < count.Count; i++) {
29+
avg[i] /= count[i];
30+
}
31+
return avg;
32+
}
33+
34+
private void Average(TreeNode node, int level, IList<int> count, IList<double> avg) {
35+
if (node == null) {
36+
return;
37+
}
38+
if (level < count.Count) {
39+
count[level]++;
40+
avg[level] += (double) node.val;
41+
} else {
42+
count.Add(1);
43+
avg.Add((double) node.val);
44+
}
45+
Average(node.left, level + 1, count, avg);
46+
Average(node.right, level + 1, count, avg);
47+
}
48+
}
49+
}
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>

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,13 +862,15 @@ C#-based LeetCode algorithm problem solutions, regularly updated.
862862
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
863863
|-|-|-|-|-|-
864864
| 0199 |[Binary Tree Right Side View](LeetCodeNet/G0101_0200/S0199_binary_tree_right_side_view/Solution.cs)| Medium | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, LeetCode_75_Binary_Tree/BFS | 0 | 100.00
865+
| 0637 |[Average of Levels in Binary Tree](LeetCodeNet/G0601_0700/S0637_average_of_levels_in_binary_tree/Solution.cs)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 1 | 100.00
865866
| 0102 |[Binary Tree Level Order Traversal](LeetCodeNet/G0101_0200/S0102_binary_tree_level_order_traversal/Solution.cs)| 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
866867
| 0103 |[Binary Tree Zigzag Level Order Traversal](LeetCodeNet/G0101_0200/S0103_binary_tree_zigzag_level_order_traversal/Solution.cs)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
867868

868869
#### Top Interview 150 Binary Search Tree
869870

870871
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
871872
|-|-|-|-|-|-
873+
| 0530 |[Minimum Absolute Difference in BST](LeetCodeNet/G0501_0600/S0530_minimum_absolute_difference_in_bst/Solution.cs)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 0 | 100.00
872874
| 0230 |[Kth Smallest Element in a BST](LeetCodeNet/G0201_0300/S0230_kth_smallest_element_in_a_bst/Solution.cs)| 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
873875
| 0098 |[Validate Binary Search Tree](LeetCodeNet/G0001_0100/S0098_validate_binary_search_tree/Solution.cs)| 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
874876

@@ -879,6 +881,7 @@ C#-based LeetCode algorithm problem solutions, regularly updated.
879881
| 0200 |[Number of Islands](LeetCodeNet/G0101_0200/S0200_number_of_islands/Solution.cs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 131 | 65.99
880882
| 0130 |[Surrounded Regions](LeetCodeNet/G0101_0200/S0130_surrounded_regions/Solution.cs)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 1 | 100.00
881883
| 0133 |[Clone Graph](LeetCodeNet/G0101_0200/S0133_clone_graph/Solution.cs)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 117 | 96.34
884+
| 0399 |[Evaluate Division](LeetCodeNet/G0301_0400/S0399_evaluate_division/Solution.cs)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path, LeetCode_75_Graphs/DFS | 3 | 66.56
882885
| 0207 |[Course Schedule](LeetCodeNet/G0201_0300/S0207_course_schedule/Solution.cs)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 4 | 91.60
883886
| 0210 |[Course Schedule II](LeetCodeNet/G0201_0300/S0210_course_schedule_ii/Solution.cs)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 4 | 91.35
884887

@@ -1762,8 +1765,10 @@ C#-based LeetCode algorithm problem solutions, regularly updated.
17621765
| 0763 |[Partition Labels](LeetCodeNet/G0701_0800/S0763_partition_labels/Solution.cs)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String, Big_O_Time_O(n)_Space_O(1) | 2 | 86.67
17631766
| 0739 |[Daily Temperatures](LeetCodeNet/G0701_0800/S0739_daily_temperatures/Solution.cs)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, LeetCode_75_Monotonic_Stack, Programming_Skills_II_Day_6, Big_O_Time_O(n)_Space_O(n) | 6 | 98.90
17641767
| 0647 |[Palindromic Substrings](LeetCodeNet/G0601_0700/S0647_palindromic_substrings/Solution.cs)| Medium | String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n) | 10 | 72.48
1768+
| 0637 |[Average of Levels in Binary Tree](LeetCodeNet/G0601_0700/S0637_average_of_levels_in_binary_tree/Solution.cs)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Top_Interview_150_Binary_Tree_BFS | 1 | 100.00
17651769
| 0560 |[Subarray Sum Equals K](LeetCodeNet/G0501_0600/S0560_subarray_sum_equals_k/Solution.cs)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array, Big_O_Time_O(n)_Space_O(n) | 12 | 90.27
17661770
| 0543 |[Diameter of Binary Tree](LeetCodeNet/G0501_0600/S0543_diameter_of_binary_tree/Solution.cs)| 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) | 0 | 100.00
1771+
| 0530 |[Minimum Absolute Difference in BST](LeetCodeNet/G0501_0600/S0530_minimum_absolute_difference_in_bst/Solution.cs)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Top_Interview_150_Binary_Search_Tree | 0 | 100.00
17671772
| 0502 |[IPO](LeetCodeNet/G0501_0600/S0502_ipo/Solution.cs)| Hard | Array, Sorting, Greedy, Heap_Priority_Queue, Top_Interview_150_Heap | 199 | 62.82
17681773
| 0494 |[Target Sum](LeetCodeNet/G0401_0500/S0494_target_sum/Solution.cs)| Medium | Array, Dynamic_Programming, Backtracking, Big_O_Time_O(n\*(sum+s))_Space_O(n\*(sum+s)) | 6 | 85.88
17691774
| 0452 |[Minimum Number of Arrows to Burst Balloons](LeetCodeNet/G0401_0500/S0452_minimum_number_of_arrows_to_burst_balloons/Solution.cs)| Medium | Array, Sorting, Greedy, LeetCode_75_Intervals, Top_Interview_150_Intervals | 51 | 49.35
@@ -1772,6 +1777,7 @@ C#-based LeetCode algorithm problem solutions, regularly updated.
17721777
| 0433 |[Minimum Genetic Mutation](LeetCodeNet/G0401_0500/S0433_minimum_genetic_mutation/Solution.cs)| Medium | String, Hash_Table, Breadth_First_Search, Graph_Theory_I_Day_12_Breadth_First_Search, Top_Interview_150_Graph_BFS | 1 | 78.46
17731778
| 0427 |[Construct Quad Tree](LeetCodeNet/G0401_0500/S0427_construct_quad_tree/Solution.cs)| Medium | Array, Tree, Matrix, Divide_and_Conquer, Top_Interview_150_Divide_and_Conquer | 91 | 66.88
17741779
| 0416 |[Partition Equal Subset Sum](LeetCodeNet/G0401_0500/S0416_partition_equal_subset_sum/Solution.cs)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 22 | 82.19
1780+
| 0399 |[Evaluate Division](LeetCodeNet/G0301_0400/S0399_evaluate_division/Solution.cs)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path, LeetCode_75_Graphs/DFS, Top_Interview_150_Graph_General | 3 | 66.56
17751781
| 0394 |[Decode String](LeetCodeNet/G0301_0400/S0394_decode_string/Solution.cs)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, LeetCode_75_Stack, Level_1_Day_14_Stack, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
17761782
| 0392 |[Is Subsequence](LeetCodeNet/G0301_0400/S0392_is_subsequence/Solution.cs)| Easy | String, Dynamic_Programming, Two_Pointers, LeetCode_75_Two_Pointers, Dynamic_Programming_I_Day_19, Level_1_Day_2_String, Udemy_Two_Pointers, Top_Interview_150_Two_Pointers | 0 | 100.00
17771783
| 0383 |[Ransom Note](LeetCodeNet/G0301_0400/S0383_ransom_note/Solution.cs)| Easy | String, Hash_Table, Counting, Data_Structure_I_Day_6_String, Top_Interview_150_Hashmap | 1 | 98.72

0 commit comments

Comments
 (0)