Skip to content

Commit a90caf9

Browse files
committed
add solution in other languages
1 parent 6a8e3d7 commit a90caf9

File tree

593 files changed

+50140
-507
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

593 files changed

+50140
-507
lines changed

c#/01_matrix.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,67 @@
1-
# 01_matrix.md
1+
# 542. [01 Matrix](https://leetcode.com/problems/01-matrix/)
2+
3+
## Approach: Breadth-First Search (BFS)
4+
5+
### Solution
6+
```csharp
7+
// Time Complexity: O(m * n), where m is the number of rows and n is the number of columns
8+
// Space Complexity: O(m * n), for the queue and distance matrix
9+
using System.Collections.Generic;
10+
11+
public class Solution
12+
{
13+
public int[][] UpdateMatrix(int[][] mat)
14+
{
15+
int rows = mat.Length;
16+
int cols = mat[0].Length;
17+
int[][] distances = new int[rows][];
18+
bool[][] visited = new bool[rows][];
19+
Queue<int[]> queue = new Queue<int[]>();
20+
21+
// Step 1: Initialize the queue with all '0' cells and mark them as visited
22+
for (int i = 0; i < rows; i++)
23+
{
24+
distances[i] = new int[cols];
25+
visited[i] = new bool[cols];
26+
for (int j = 0; j < cols; j++)
27+
{
28+
if (mat[i][j] == 0)
29+
{
30+
queue.Enqueue(new int[] { i, j });
31+
visited[i][j] = true;
32+
}
33+
}
34+
}
35+
36+
// Step 2: Perform BFS to calculate distances
37+
int[][] directions = new int[][]
38+
{
39+
new int[] { 0, 1 },
40+
new int[] { 1, 0 },
41+
new int[] { 0, -1 },
42+
new int[] { -1, 0 }
43+
};
44+
45+
while (queue.Count > 0)
46+
{
47+
int[] current = queue.Dequeue();
48+
foreach (int[] dir in directions)
49+
{
50+
int newRow = current[0] + dir[0];
51+
int newCol = current[1] + dir[1];
52+
53+
// Check bounds and if the cell is not visited
54+
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && !visited[newRow][newCol])
55+
{
56+
distances[newRow][newCol] = distances[current[0]][current[1]] + 1;
57+
queue.Enqueue(new int[] { newRow, newCol });
58+
visited[newRow][newCol] = true;
59+
}
60+
}
61+
}
62+
63+
return distances;
64+
}
65+
}
66+
```
267

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
1-
# all_paths_from_source_to_target.md
1+
# 797. [All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target/)
2+
3+
## Approach: Depth-First Search (DFS)
4+
5+
### Solution
6+
csharp
7+
```csharp
8+
// Time Complexity: O(2^n * n), where n is the number of nodes in the graph
9+
// Space Complexity: O(2^n * n), for storing all paths in the result
10+
using System.Collections.Generic;
11+
12+
public class Solution {
13+
public IList<IList<int>> AllPathsSourceTarget(int[][] graph) {
14+
List<IList<int>> result = new List<IList<int>>();
15+
List<int> path = new List<int>();
16+
path.Add(0); // Start from the source node
17+
DFS(graph, 0, path, result);
18+
return result;
19+
}
20+
21+
private void DFS(int[][] graph, int node, List<int> path, List<IList<int>> result) {
22+
if (node == graph.Length - 1) {
23+
// If we reach the target node, add the current path to the result
24+
result.Add(new List<int>(path));
25+
return;
26+
}
27+
28+
foreach (int neighbor in graph[node]) {
29+
path.Add(neighbor); // Add the neighbor to the current path
30+
DFS(graph, neighbor, path, result); // Recur for the neighbor
31+
path.RemoveAt(path.Count - 1); // Backtrack to explore other paths
32+
}
33+
}
34+
}
35+
```
236

c#/binary_search_tree_iterator.md

Lines changed: 176 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,177 @@
1-
# binary_search_tree_iterator.md
1+
# 173. [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)
2+
3+
## Approach 1: Controlled Recursion Using Stack
4+
5+
### Solution
6+
```csharp
7+
// Time Complexity:
8+
// - Constructor: O(h), where h is the height of the tree
9+
// - Next(): O(1) on average across all calls
10+
// - HasNext(): O(1)
11+
// Space Complexity: O(h), where h is the height of the tree for the stack
12+
using System.Collections.Generic;
13+
14+
public class BSTIterator
15+
{
16+
private Stack<TreeNode> stack;
17+
18+
public BSTIterator(TreeNode root)
19+
{
20+
stack = new Stack<TreeNode>();
21+
PushLeft(root);
22+
}
23+
24+
// Push all left children of the current node to the stack
25+
private void PushLeft(TreeNode node)
26+
{
27+
while (node != null)
28+
{
29+
stack.Push(node);
30+
node = node.left;
31+
}
32+
}
33+
34+
/** @return the next smallest number */
35+
public int Next()
36+
{
37+
TreeNode node = stack.Pop(); // Get the topmost node (smallest available)
38+
if (node.right != null)
39+
{
40+
PushLeft(node.right); // Process the right subtree
41+
}
42+
return node.val;
43+
}
44+
45+
/** @return whether we have a next smallest number */
46+
public bool HasNext()
47+
{
48+
return stack.Count > 0; // Check if the stack is non-empty
49+
}
50+
}
51+
52+
public class TreeNode
53+
{
54+
public int val;
55+
public TreeNode left;
56+
public TreeNode right;
57+
public TreeNode(int x) { val = x; }
58+
}
59+
```
60+
61+
## Approach 2: Precomputed Inorder Traversal
62+
63+
### Solution
64+
```csharp
65+
// Time Complexity:
66+
// - Constructor: O(n), where n is the number of nodes in the tree
67+
// - Next(): O(1)
68+
// - HasNext(): O(1)
69+
// Space Complexity: O(n), where n is the number of nodes in the tree
70+
using System.Collections.Generic;
71+
72+
public class BSTIterator
73+
{
74+
private List<int> inorder;
75+
private int index;
76+
77+
public BSTIterator(TreeNode root)
78+
{
79+
inorder = new List<int>();
80+
index = 0;
81+
InorderTraversal(root);
82+
}
83+
84+
// Perform an inorder traversal to store the elements
85+
private void InorderTraversal(TreeNode node)
86+
{
87+
if (node == null)
88+
{
89+
return;
90+
}
91+
InorderTraversal(node.left);
92+
inorder.Add(node.val);
93+
InorderTraversal(node.right);
94+
}
95+
96+
/** @return the next smallest number */
97+
public int Next()
98+
{
99+
return inorder[index++];
100+
}
101+
102+
/** @return whether we have a next smallest number */
103+
public bool HasNext()
104+
{
105+
return index < inorder.Count;
106+
}
107+
}
108+
```
109+
110+
## Approach 3: Morris Traversal (Space Optimized, Less Practical for Iterator)
111+
112+
### Solution
113+
```csharp
114+
// Time Complexity:
115+
// - Constructor: O(1) (no precomputation)
116+
// - Next(): O(1) on average across all calls
117+
// - HasNext(): O(1)
118+
// Space Complexity: O(1), no additional storage
119+
public class BSTIterator
120+
{
121+
private TreeNode current;
122+
private TreeNode predecessor;
123+
124+
public BSTIterator(TreeNode root)
125+
{
126+
current = root;
127+
}
128+
129+
/** @return the next smallest number */
130+
public int Next()
131+
{
132+
int value = -1;
133+
134+
while (current != null)
135+
{
136+
if (current.left == null)
137+
{
138+
value = current.val;
139+
current = current.right; // Move to the right subtree
140+
break;
141+
}
142+
else
143+
{
144+
predecessor = current.left;
145+
146+
// Find the rightmost node in the left subtree
147+
while (predecessor.right != null && predecessor.right != current)
148+
{
149+
predecessor = predecessor.right;
150+
}
151+
152+
if (predecessor.right == null)
153+
{
154+
predecessor.right = current; // Create a temporary link
155+
current = current.left;
156+
}
157+
else
158+
{
159+
predecessor.right = null; // Remove the temporary link
160+
value = current.val;
161+
current = current.right; // Move to the right subtree
162+
break;
163+
}
164+
}
165+
}
166+
167+
return value;
168+
}
169+
170+
/** @return whether we have a next smallest number */
171+
public bool HasNext()
172+
{
173+
return current != null;
174+
}
175+
}
176+
```
2177

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,103 @@
1-
# binary_tree_inorder_traversal.md
1+
# 94. [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)
2+
3+
## Approach 1: Recursive Inorder Traversal
4+
5+
### Solution
6+
csharp
7+
```csharp
8+
// Time Complexity: O(n), where n is the number of nodes in the tree
9+
// Space Complexity: O(h), where h is the height of the tree for recursion stack
10+
using System.Collections.Generic;
11+
12+
public class Solution {
13+
public IList<int> InorderTraversal(TreeNode root) {
14+
List<int> result = new List<int>();
15+
Inorder(root, result);
16+
return result;
17+
}
18+
19+
private void Inorder(TreeNode node, List<int> result) {
20+
if (node == null) {
21+
return; // Base case: if the node is null, return
22+
}
23+
24+
Inorder(node.left, result); // Recurse for the left subtree
25+
result.Add(node.val); // Visit the root
26+
Inorder(node.right, result); // Recurse for the right subtree
27+
}
28+
}
29+
```
30+
31+
## Approach 2: Iterative Inorder Traversal Using Stack
32+
33+
### Solution
34+
csharp
35+
```csharp
36+
// Time Complexity: O(n), where n is the number of nodes in the tree
37+
// Space Complexity: O(h), where h is the height of the tree for the stack
38+
using System.Collections.Generic;
39+
40+
public class Solution {
41+
public IList<int> InorderTraversal(TreeNode root) {
42+
List<int> result = new List<int>();
43+
Stack<TreeNode> stack = new Stack<TreeNode>();
44+
TreeNode current = root;
45+
46+
while (current != null || stack.Count > 0) {
47+
while (current != null) {
48+
stack.Push(current); // Push the current node and move to the left subtree
49+
current = current.left;
50+
}
51+
52+
current = stack.Pop(); // Process the top node
53+
result.Add(current.val); // Visit the node
54+
current = current.right; // Move to the right subtree
55+
}
56+
57+
return result;
58+
}
59+
}
60+
```
61+
62+
## Approach 3: Morris Inorder Traversal (Without Recursion or Stack)
63+
64+
### Solution
65+
csharp
66+
```csharp
67+
// Time Complexity: O(n), where n is the number of nodes in the tree
68+
// Space Complexity: O(1), as no additional space is used
69+
using System.Collections.Generic;
70+
71+
public class Solution {
72+
public IList<int> InorderTraversal(TreeNode root) {
73+
List<int> result = new List<int>();
74+
TreeNode current = root;
75+
76+
while (current != null) {
77+
if (current.left == null) {
78+
result.Add(current.val); // Visit the node
79+
current = current.right; // Move to the right subtree
80+
} else {
81+
TreeNode predecessor = current.left;
82+
83+
// Find the rightmost node in the left subtree
84+
while (predecessor.right != null && predecessor.right != current) {
85+
predecessor = predecessor.right;
86+
}
87+
88+
if (predecessor.right == null) {
89+
predecessor.right = current; // Create a temporary thread to the root
90+
current = current.left; // Move to the left subtree
91+
} else {
92+
predecessor.right = null; // Remove the temporary thread
93+
result.Add(current.val); // Visit the node
94+
current = current.right; // Move to the right subtree
95+
}
96+
}
97+
}
98+
99+
return result;
100+
}
101+
}
102+
```
2103

0 commit comments

Comments
 (0)