Skip to content

Added tasks 102-238 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added tasks 102-238
  • Loading branch information
javadev committed Jan 11, 2024
commit 3aac358a4c40a8b805facf8cdc64961e79d771a0
10 changes: 10 additions & 0 deletions LeetCodeNet/G0001_0100/S0002_add_two_numbers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ You may assume the two numbers do not contain any leading zero, except the numbe
```csharp
using LeetCodeNet.Com_github_leetcode;

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class Solution {
int maxArea = -1;
int left = 0;
int right = height.Length - 1;

while (left < right) {
if (height[left] < height[right]) {
maxArea = Math.Max(maxArea, height[left] * (right - left));
Expand All @@ -61,7 +60,6 @@ public class Solution {
right--;
}
}

return maxArea;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ Given the `head` of a linked list, remove the `nth` node from the end of the lis
```csharp
using LeetCodeNet.Com_github_leetcode;

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
private int n;

Expand Down
11 changes: 11 additions & 0 deletions LeetCodeNet/G0001_0100/S0021_merge_two_sorted_lists/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ Merge two sorted linked lists and return it as a **sorted** list. The list shoul
```csharp
using LeetCodeNet.Com_github_leetcode;

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
ListNode list = new ListNode(-1);
Expand Down
11 changes: 11 additions & 0 deletions LeetCodeNet/G0001_0100/S0023_merge_k_sorted_lists/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ _Merge all the linked-lists into one sorted linked-list and return it._
```csharp
using LeetCodeNet.Com_github_leetcode;

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode MergeKLists(ListNode[] lists) {
if (lists.Length == 0) {
Expand Down
11 changes: 11 additions & 0 deletions LeetCodeNet/G0001_0100/S0024_swap_nodes_in_pairs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ Given a linked list, swap every two adjacent nodes and return its head. You must
```csharp
using LeetCodeNet.Com_github_leetcode;

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode SwapPairs(ListNode head) {
if (head == null) {
Expand Down
11 changes: 11 additions & 0 deletions LeetCodeNet/G0001_0100/S0025_reverse_nodes_in_k_group/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ You may not alter the values in the list's nodes, only nodes themselves may be c
```csharp
using LeetCodeNet.Com_github_leetcode;

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode ReverseKGroup(ListNode head, int k) {
if (head == null || head.next == null || k == 1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[![](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)
[![](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)

## 102\. Binary Tree Level Order Traversal

Medium

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).

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)

**Input:** root = [3,9,20,null,null,15,7]

**Output:** [[3],[9,20],[15,7]]

**Example 2:**

**Input:** root = [1]

**Output:** [[1]]

**Example 3:**

**Input:** root = []

**Output:** []

**Constraints:**

* The number of nodes in the tree is in the range `[0, 2000]`.
* `-1000 <= Node.val <= 1000`

## Solution

```csharp
using LeetCodeNet.Com_github_leetcode;

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public IList<IList<int>> LevelOrder(TreeNode root) {
IList<IList<int>> result = new List<IList<int>>();
if (root == null) {
return result;
}
Queue<TreeNode> queue = new Queue<TreeNode>();
queue.Enqueue(root);
queue.Enqueue(null);
List<int> level = new List<int>();
while (queue.Count > 0) {
root = queue.Dequeue();
while (queue.Count > 0 && root != null) {
level.Add((int)root.val);
if (root.left != null) {
queue.Enqueue(root.left);
}
if (root.right != null) {
queue.Enqueue(root.right);
}
root = queue.Dequeue();
}
result.Add(level);
level = new List<int>();
if (queue.Count > 0) {
queue.Enqueue(null);
}
}
return result;
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
[![](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)
[![](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)

## 104\. Maximum Depth of Binary Tree

Easy

Given the `root` of a binary tree, return _its maximum depth_.

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.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg)

**Input:** root = [3,9,20,null,null,15,7]

**Output:** 3

**Example 2:**

**Input:** root = [1,null,2]

**Output:** 2

**Example 3:**

**Input:** root = []

**Output:** 0

**Example 4:**

**Input:** root = [0]

**Output:** 1

**Constraints:**

* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
* `-100 <= Node.val <= 100`

## Solution

```csharp
using LeetCodeNet.Com_github_leetcode;

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int MaxDepth(TreeNode root) {
return FindDepth(root, 0);
}

private int FindDepth(TreeNode node, int currentDepth) {
if (node == null) {
return 0;
}
currentDepth++;
return 1
+ Math.Max(FindDepth(node.left, currentDepth), FindDepth(node.right, currentDepth));
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[![](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)
[![](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)

## 105\. Construct Binary Tree from Preorder and Inorder Traversal

Medium

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_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg)

**Input:** preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]

**Output:** [3,9,20,null,null,15,7]

**Example 2:**

**Input:** preorder = [-1], inorder = [-1]

**Output:** [-1]

**Constraints:**

* `1 <= preorder.length <= 3000`
* `inorder.length == preorder.length`
* `-3000 <= preorder[i], inorder[i] <= 3000`
* `preorder` and `inorder` consist of **unique** values.
* Each value of `inorder` also appears in `preorder`.
* `preorder` is **guaranteed** to be the preorder traversal of the tree.
* `inorder` is **guaranteed** to be the inorder traversal of the tree.

## Solution

```csharp
using LeetCodeNet.Com_github_leetcode;

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private int j;
private Dictionary<int, int> map = new Dictionary<int, int>();

public int Get(int key) {
return map[key];
}

private TreeNode Answer(int[] preorder, int[] inorder, int start, int end) {
if (start > end || j > preorder.Length) {
return null;
}
int value = preorder[j++];
int index = Get(value);
TreeNode node = new TreeNode(value);
node.left = Answer(preorder, inorder, start, index - 1);
node.right = Answer(preorder, inorder, index + 1, end);
return node;
}

public TreeNode BuildTree(int[] preorder, int[] inorder) {
j = 0;
for (int i = 0; i < preorder.Length; i++) {
map.Add(inorder[i], i);
}
return Answer(preorder, inorder, 0, preorder.Length - 1);
}
}
```
Loading