Skip to content

Commit 5a83ca4

Browse files
authored
Added tasks 102-238
1 parent 1dea4ef commit 5a83ca4

File tree

42 files changed

+2763
-25
lines changed

Some content is hidden

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

42 files changed

+2763
-25
lines changed

LeetCodeNet/G0001_0100/S0002_add_two_numbers/readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ You may assume the two numbers do not contain any leading zero, except the numbe
4242
```csharp
4343
using LeetCodeNet.Com_github_leetcode;
4444

45+
/**
46+
* Definition for singly-linked list.
47+
* public class ListNode {
48+
* int val;
49+
* ListNode next;
50+
* ListNode() {}
51+
* ListNode(int val) { this.val = val; }
52+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
53+
* }
54+
*/
4555
public class Solution {
4656
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
4757
ListNode dummyHead = new ListNode(0);

LeetCodeNet/G0001_0100/S0011_container_with_most_water/readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public class Solution {
5151
int maxArea = -1;
5252
int left = 0;
5353
int right = height.Length - 1;
54-
5554
while (left < right) {
5655
if (height[left] < height[right]) {
5756
maxArea = Math.Max(maxArea, height[left] * (right - left));
@@ -61,7 +60,6 @@ public class Solution {
6160
right--;
6261
}
6362
}
64-
6563
return maxArea;
6664
}
6765
}

LeetCodeNet/G0001_0100/S0019_remove_nth_node_from_end_of_list/readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ Given the `head` of a linked list, remove the `nth` node from the end of the lis
4141
```csharp
4242
using LeetCodeNet.Com_github_leetcode;
4343

44+
/**
45+
* Definition for singly-linked list.
46+
* public class ListNode {
47+
* public int val;
48+
* public ListNode next;
49+
* public ListNode(int val=0, ListNode next=null) {
50+
* this.val = val;
51+
* this.next = next;
52+
* }
53+
* }
54+
*/
4455
public class Solution {
4556
private int n;
4657

LeetCodeNet/G0001_0100/S0021_merge_two_sorted_lists/readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ Merge two sorted linked lists and return it as a **sorted** list. The list shoul
3838
```csharp
3939
using LeetCodeNet.Com_github_leetcode;
4040

41+
/**
42+
* Definition for singly-linked list.
43+
* public class ListNode {
44+
* public int val;
45+
* public ListNode next;
46+
* public ListNode(int val=0, ListNode next=null) {
47+
* this.val = val;
48+
* this.next = next;
49+
* }
50+
* }
51+
*/
4152
public class Solution {
4253
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
4354
ListNode list = new ListNode(-1);

LeetCodeNet/G0001_0100/S0023_merge_k_sorted_lists/readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ _Merge all the linked-lists into one sorted linked-list and return it._
4343
```csharp
4444
using LeetCodeNet.Com_github_leetcode;
4545

46+
/**
47+
* Definition for singly-linked list.
48+
* public class ListNode {
49+
* public int val;
50+
* public ListNode next;
51+
* public ListNode(int val=0, ListNode next=null) {
52+
* this.val = val;
53+
* this.next = next;
54+
* }
55+
* }
56+
*/
4657
public class Solution {
4758
public ListNode MergeKLists(ListNode[] lists) {
4859
if (lists.Length == 0) {

LeetCodeNet/G0001_0100/S0024_swap_nodes_in_pairs/readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ Given a linked list, swap every two adjacent nodes and return its head. You must
3737
```csharp
3838
using LeetCodeNet.Com_github_leetcode;
3939

40+
/**
41+
* Definition for singly-linked list.
42+
* public class ListNode {
43+
* public int val;
44+
* public ListNode next;
45+
* public ListNode(int val=0, ListNode next=null) {
46+
* this.val = val;
47+
* this.next = next;
48+
* }
49+
* }
50+
*/
4051
public class Solution {
4152
public ListNode SwapPairs(ListNode head) {
4253
if (head == null) {

LeetCodeNet/G0001_0100/S0025_reverse_nodes_in_k_group/readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ You may not alter the values in the list's nodes, only nodes themselves may be c
5353
```csharp
5454
using LeetCodeNet.Com_github_leetcode;
5555

56+
/**
57+
* Definition for singly-linked list.
58+
* public class ListNode {
59+
* public int val;
60+
* public ListNode next;
61+
* public ListNode(int val=0, ListNode next=null) {
62+
* this.val = val;
63+
* this.next = next;
64+
* }
65+
* }
66+
*/
5667
public class Solution {
5768
public ListNode ReverseKGroup(ListNode head, int k) {
5869
if (head == null || head.next == null || k == 1) {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 102\. Binary Tree Level Order Traversal
5+
6+
Medium
7+
8+
Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)
13+
14+
**Input:** root = [3,9,20,null,null,15,7]
15+
16+
**Output:** [[3],[9,20],[15,7]]
17+
18+
**Example 2:**
19+
20+
**Input:** root = [1]
21+
22+
**Output:** [[1]]
23+
24+
**Example 3:**
25+
26+
**Input:** root = []
27+
28+
**Output:** []
29+
30+
**Constraints:**
31+
32+
* The number of nodes in the tree is in the range `[0, 2000]`.
33+
* `-1000 <= Node.val <= 1000`
34+
35+
## Solution
36+
37+
```csharp
38+
using LeetCodeNet.Com_github_leetcode;
39+
40+
/**
41+
* Definition for a binary tree node.
42+
* public class TreeNode {
43+
* public int val;
44+
* public TreeNode left;
45+
* public TreeNode right;
46+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
47+
* this.val = val;
48+
* this.left = left;
49+
* this.right = right;
50+
* }
51+
* }
52+
*/
53+
public class Solution {
54+
public IList<IList<int>> LevelOrder(TreeNode root) {
55+
IList<IList<int>> result = new List<IList<int>>();
56+
if (root == null) {
57+
return result;
58+
}
59+
Queue<TreeNode> queue = new Queue<TreeNode>();
60+
queue.Enqueue(root);
61+
queue.Enqueue(null);
62+
List<int> level = new List<int>();
63+
while (queue.Count > 0) {
64+
root = queue.Dequeue();
65+
while (queue.Count > 0 && root != null) {
66+
level.Add((int)root.val);
67+
if (root.left != null) {
68+
queue.Enqueue(root.left);
69+
}
70+
if (root.right != null) {
71+
queue.Enqueue(root.right);
72+
}
73+
root = queue.Dequeue();
74+
}
75+
result.Add(level);
76+
level = new List<int>();
77+
if (queue.Count > 0) {
78+
queue.Enqueue(null);
79+
}
80+
}
81+
return result;
82+
}
83+
}
84+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 104\. Maximum Depth of Binary Tree
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, return _its maximum depth_.
9+
10+
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg)
15+
16+
**Input:** root = [3,9,20,null,null,15,7]
17+
18+
**Output:** 3
19+
20+
**Example 2:**
21+
22+
**Input:** root = [1,null,2]
23+
24+
**Output:** 2
25+
26+
**Example 3:**
27+
28+
**Input:** root = []
29+
30+
**Output:** 0
31+
32+
**Example 4:**
33+
34+
**Input:** root = [0]
35+
36+
**Output:** 1
37+
38+
**Constraints:**
39+
40+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
41+
* `-100 <= Node.val <= 100`
42+
43+
## Solution
44+
45+
```csharp
46+
using LeetCodeNet.Com_github_leetcode;
47+
48+
/**
49+
* Definition for a binary tree node.
50+
* public class TreeNode {
51+
* public int val;
52+
* public TreeNode left;
53+
* public TreeNode right;
54+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
55+
* this.val = val;
56+
* this.left = left;
57+
* this.right = right;
58+
* }
59+
* }
60+
*/
61+
public class Solution {
62+
public int MaxDepth(TreeNode root) {
63+
return FindDepth(root, 0);
64+
}
65+
66+
private int FindDepth(TreeNode node, int currentDepth) {
67+
if (node == null) {
68+
return 0;
69+
}
70+
currentDepth++;
71+
return 1
72+
+ Math.Max(FindDepth(node.left, currentDepth), FindDepth(node.right, currentDepth));
73+
}
74+
}
75+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 105\. Construct Binary Tree from Preorder and Inorder Traversal
5+
6+
Medium
7+
8+
Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg)
13+
14+
**Input:** preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
15+
16+
**Output:** [3,9,20,null,null,15,7]
17+
18+
**Example 2:**
19+
20+
**Input:** preorder = [-1], inorder = [-1]
21+
22+
**Output:** [-1]
23+
24+
**Constraints:**
25+
26+
* `1 <= preorder.length <= 3000`
27+
* `inorder.length == preorder.length`
28+
* `-3000 <= preorder[i], inorder[i] <= 3000`
29+
* `preorder` and `inorder` consist of **unique** values.
30+
* Each value of `inorder` also appears in `preorder`.
31+
* `preorder` is **guaranteed** to be the preorder traversal of the tree.
32+
* `inorder` is **guaranteed** to be the inorder traversal of the tree.
33+
34+
## Solution
35+
36+
```csharp
37+
using LeetCodeNet.Com_github_leetcode;
38+
39+
/**
40+
* Definition for a binary tree node.
41+
* public class TreeNode {
42+
* public int val;
43+
* public TreeNode left;
44+
* public TreeNode right;
45+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
46+
* this.val = val;
47+
* this.left = left;
48+
* this.right = right;
49+
* }
50+
* }
51+
*/
52+
public class Solution {
53+
private int j;
54+
private Dictionary<int, int> map = new Dictionary<int, int>();
55+
56+
public int Get(int key) {
57+
return map[key];
58+
}
59+
60+
private TreeNode Answer(int[] preorder, int[] inorder, int start, int end) {
61+
if (start > end || j > preorder.Length) {
62+
return null;
63+
}
64+
int value = preorder[j++];
65+
int index = Get(value);
66+
TreeNode node = new TreeNode(value);
67+
node.left = Answer(preorder, inorder, start, index - 1);
68+
node.right = Answer(preorder, inorder, index + 1, end);
69+
return node;
70+
}
71+
72+
public TreeNode BuildTree(int[] preorder, int[] inorder) {
73+
j = 0;
74+
for (int i = 0; i < preorder.Length; i++) {
75+
map.Add(inorder[i], i);
76+
}
77+
return Answer(preorder, inorder, 0, preorder.Length - 1);
78+
}
79+
}
80+
```

0 commit comments

Comments
 (0)