Skip to content

Commit 5211e6f

Browse files
authored
Added tasks 103-117
1 parent 3f28b22 commit 5211e6f

File tree

16 files changed

+576
-0
lines changed

16 files changed

+576
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace LeetCodeNet.G0101_0200.S0103_binary_tree_zigzag_level_order_traversal {
2+
3+
using LeetCodeNet.Com_github_leetcode;
4+
using Xunit;
5+
using System.Collections.Generic;
6+
7+
public class SolutionTest {
8+
[Fact]
9+
public void ZigzagLevelOrder() {
10+
var root = TreeNode.Create(new List<int?> {3, 9, 20, null, null, 15, 7});
11+
var expected = new List<IList<int>> {
12+
new List<int> {3},
13+
new List<int> {20, 9},
14+
new List<int> {15, 7}
15+
};
16+
Assert.Equal(expected, new Solution().ZigzagLevelOrder(root));
17+
}
18+
19+
[Fact]
20+
public void ZigzagLevelOrder2() {
21+
var root = TreeNode.Create(new List<int?> {1});
22+
var expected = new List<IList<int>> {
23+
new List<int> {1}
24+
};
25+
Assert.Equal(expected, new Solution().ZigzagLevelOrder(root));
26+
}
27+
28+
[Fact]
29+
public void ZigzagLevelOrder3() {
30+
var expected = new List<IList<int>>();
31+
Assert.Equal(expected, new Solution().ZigzagLevelOrder(null));
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace LeetCodeNet.G0101_0200.S0106_construct_binary_tree_from_inorder_and_postorder_traversal {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void ConstructBinaryTree() {
9+
int[] inorder = {9, 3, 15, 20, 7};
10+
int[] postorder = {9, 15, 7, 20, 3};
11+
TreeNode actual = new Solution().BuildTree(inorder, postorder);
12+
Assert.Equal("3,9,20,15,7", actual.ToString());
13+
}
14+
15+
[Fact]
16+
public void ConstructBinaryTree2() {
17+
int[] inorder = {-1};
18+
int[] postorder = {-1};
19+
TreeNode actual = new Solution().BuildTree(inorder, postorder);
20+
Assert.Equal("-1", actual.ToString());
21+
}
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace LeetCodeNet.G0101_0200.S0108_convert_sorted_array_to_binary_search_tree {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void SortedArrayToBST() {
9+
var actual = new Solution().SortedArrayToBST(new int[] {-10, -3, 0, 5, 9});
10+
Assert.Equal("0,-10,null,-3,5,null,9", actual.ToString());
11+
}
12+
13+
[Fact]
14+
public void SortedArrayToBST2() {
15+
var actual = new Solution().SortedArrayToBST(new int[] {1, 3});
16+
Assert.Equal("1,null,3", actual.ToString());
17+
}
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace LeetCodeNet.G0101_0200.S0112_path_sum {
2+
3+
using Xunit;
4+
using System.Collections.Generic;
5+
using LeetCodeNet.Com_github_leetcode;
6+
7+
public class SolutionTest {
8+
[Fact]
9+
public void HasPathSum() {
10+
var root = TreeNode.Create(new List<int?> {5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1});
11+
Assert.True(new Solution().HasPathSum(root, 22));
12+
}
13+
14+
[Fact]
15+
public void HasPathSum2() {
16+
var root = TreeNode.Create(new List<int?> {1, 2, 3});
17+
Assert.False(new Solution().HasPathSum(root, 22));
18+
}
19+
20+
[Fact]
21+
public void HasPathSum3() {
22+
Assert.False(new Solution().HasPathSum(null, 0));
23+
}
24+
}
25+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace LeetCodeNet.G0101_0200.S0117_populating_next_right_pointers_in_each_node_ii {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void Connect() {
8+
Assert.Null(new Solution().Connect(null));
9+
}
10+
11+
[Fact]
12+
public void Connect2() {
13+
var node = new Node(
14+
1,
15+
new Node(2, new Node(4), new Node(5), null),
16+
new Node(3, null, new Node(7), null),
17+
null);
18+
19+
var node7 = new Node(7);
20+
var node3 = new Node(3, null, node7, null);
21+
var node5 = new Node(5, null, null, node7);
22+
var node4 = new Node(4, null, null, node5);
23+
var node2 = new Node(2, node4, node5, node3);
24+
var node1 = new Node(1, node2, node3, null);
25+
26+
Assert.Equal(node1.ToString(), new Solution().Connect(node).ToString());
27+
}
28+
29+
[Fact]
30+
public void Connect3() {
31+
var node = new Node(
32+
1,
33+
new Node(2, new Node(4, new Node(7), null, null), new Node(5), null),
34+
new Node(3, null, new Node(6, null, new Node(8), null), null),
35+
null);
36+
37+
var node8 = new Node(8, null, null, null);
38+
var node7 = new Node(7, null, null, node8);
39+
var node6 = new Node(6, null, node8, null);
40+
var node3 = new Node(3, null, node6, null);
41+
var node5 = new Node(5, null, null, node6);
42+
var node4 = new Node(4, node7, null, node5);
43+
var node2 = new Node(2, node4, node5, node3);
44+
var node1 = new Node(1, node2, node3, null);
45+
46+
Assert.Equal(node1.ToString(), new Solution().Connect(node).ToString());
47+
}
48+
}
49+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace LeetCodeNet.G0101_0200.S0103_binary_tree_zigzag_level_order_traversal {
2+
3+
// #Medium #Top_Interview_Questions #Breadth_First_Search #Tree #Binary_Tree
4+
// #Data_Structure_II_Day_15_Tree #Udemy_Tree_Stack_Queue #Top_Interview_150_Binary_Tree_BFS
5+
// #2025_07_09_Time_0_ms_(100.00%)_Space_46.86_MB_(87.33%)
6+
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<IList<int>> ZigzagLevelOrder(TreeNode root) {
25+
var queue = new Queue<TreeNode>();
26+
var results = new List<IList<int>>();
27+
if (root == null) {
28+
return results;
29+
}
30+
var level = new List<int>();
31+
queue.Enqueue(root);
32+
queue.Enqueue(null);
33+
var d = false;
34+
while (queue.Count > 0) {
35+
var c = queue.Dequeue();
36+
if (c == null) {
37+
if (d) {
38+
level.Reverse();
39+
}
40+
results.Add(level);
41+
if (queue.Count == 0) {
42+
break;
43+
} else {
44+
queue.Enqueue(null);
45+
level = new List<int>();
46+
d = !d;
47+
}
48+
} else {
49+
level.Add((int)c.val);
50+
if (c.left != null) {
51+
queue.Enqueue(c.left);
52+
}
53+
if (c.right != null) {
54+
queue.Enqueue(c.right);
55+
}
56+
}
57+
}
58+
return results;
59+
}
60+
}
61+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
103\. Binary Tree Zigzag Level Order Traversal
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg)
10+
11+
**Input:** root = [3,9,20,null,null,15,7]
12+
13+
**Output:** [[3],[20,9],[15,7]]
14+
15+
**Example 2:**
16+
17+
**Input:** root = [1]
18+
19+
**Output:** [[1]]
20+
21+
**Example 3:**
22+
23+
**Input:** root = []
24+
25+
**Output:** []
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the tree is in the range `[0, 2000]`.
30+
* `-100 <= Node.val <= 100`
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace LeetCodeNet.G0101_0200.S0106_construct_binary_tree_from_inorder_and_postorder_traversal {
2+
3+
// #Medium #Array #Hash_Table #Tree #Binary_Tree #Divide_and_Conquer
4+
// #Top_Interview_150_Binary_Tree_General #2025_07_09_Time_0_ms_(100.00%)_Space_44.29_MB_(90.60%)
5+
6+
using LeetCodeNet.Com_github_leetcode;
7+
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* public int val;
12+
* public TreeNode left;
13+
* public TreeNode right;
14+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
15+
* this.val = val;
16+
* this.left = left;
17+
* this.right = right;
18+
* }
19+
* }
20+
*/
21+
public class Solution {
22+
public TreeNode BuildTree(int[] inorder, int[] postorder) {
23+
int inIndex = inorder.Length - 1;
24+
int postIndex = postorder.Length - 1;
25+
return Helper(inorder, postorder, ref inIndex, ref postIndex, int.MaxValue);
26+
}
27+
28+
private TreeNode Helper(int[] inorder, int[] postorder, ref int inIndex, ref int postIndex, int target) {
29+
if (inIndex < 0 || inorder[inIndex] == target) {
30+
return null;
31+
}
32+
TreeNode root = new TreeNode(postorder[postIndex--]);
33+
root.right = Helper(inorder, postorder, ref inIndex, ref postIndex, (int)root.val);
34+
inIndex--;
35+
root.left = Helper(inorder, postorder, ref inIndex, ref postIndex, target);
36+
return root;
37+
}
38+
}
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
106\. Construct Binary Tree from Inorder and Postorder Traversal
2+
3+
Medium
4+
5+
Given two integer arrays `inorder` and `postorder` where `inorder` is the inorder traversal of a binary tree and `postorder` is the postorder traversal of the same tree, construct and return _the binary tree_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg)
10+
11+
**Input:** inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
12+
13+
**Output:** [3,9,20,null,null,15,7]
14+
15+
**Example 2:**
16+
17+
**Input:** inorder = [-1], postorder = [-1]
18+
19+
**Output:** [-1]
20+
21+
**Constraints:**
22+
23+
* `1 <= inorder.length <= 3000`
24+
* `postorder.length == inorder.length`
25+
* `-3000 <= inorder[i], postorder[i] <= 3000`
26+
* `inorder` and `postorder` consist of **unique** values.
27+
* Each value of `postorder` also appears in `inorder`.
28+
* `inorder` is **guaranteed** to be the inorder traversal of the tree.
29+
* `postorder` is **guaranteed** to be the postorder traversal of the tree.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace LeetCodeNet.G0101_0200.S0108_convert_sorted_array_to_binary_search_tree {
2+
3+
// #Easy #Top_Interview_Questions #Array #Tree #Binary_Tree #Binary_Search_Tree #Divide_and_Conquer
4+
// #Data_Structure_II_Day_15_Tree #Level_2_Day_9_Binary_Search_Tree #Udemy_Tree_Stack_Queue
5+
// #Top_Interview_150_Divide_and_Conquer #2025_07_09_Time_0_ms_(100.00%)_Space_45.24_MB_(54.95%)
6+
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+
public TreeNode SortedArrayToBST(int[] nums) {
24+
return MakeTree(nums, 0, nums.Length - 1);
25+
}
26+
27+
private TreeNode MakeTree(int[] nums, int left, int right) {
28+
if (left > right) {
29+
return null;
30+
}
31+
int mid = (left + right) / 2;
32+
TreeNode midNode = new TreeNode(nums[mid]);
33+
midNode.left = MakeTree(nums, left, mid - 1);
34+
midNode.right = MakeTree(nums, mid + 1, right);
35+
return midNode;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)