Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5211e6f

Browse files
authored
Added tasks 103-117
1 parent3f28b22 commit5211e6f

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+
namespaceLeetCodeNet.G0101_0200.S0103_binary_tree_zigzag_level_order_traversal{
2+
3+
usingLeetCodeNet.Com_github_leetcode;
4+
usingXunit;
5+
usingSystem.Collections.Generic;
6+
7+
publicclassSolutionTest{
8+
[Fact]
9+
publicvoidZigzagLevelOrder(){
10+
varroot=TreeNode.Create(newList<int?>{3,9,20,null,null,15,7});
11+
varexpected=newList<IList<int>>{
12+
newList<int>{3},
13+
newList<int>{20,9},
14+
newList<int>{15,7}
15+
};
16+
Assert.Equal(expected,newSolution().ZigzagLevelOrder(root));
17+
}
18+
19+
[Fact]
20+
publicvoidZigzagLevelOrder2(){
21+
varroot=TreeNode.Create(newList<int?>{1});
22+
varexpected=newList<IList<int>>{
23+
newList<int>{1}
24+
};
25+
Assert.Equal(expected,newSolution().ZigzagLevelOrder(root));
26+
}
27+
28+
[Fact]
29+
publicvoidZigzagLevelOrder3(){
30+
varexpected=newList<IList<int>>();
31+
Assert.Equal(expected,newSolution().ZigzagLevelOrder(null));
32+
}
33+
}
34+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespaceLeetCodeNet.G0101_0200.S0106_construct_binary_tree_from_inorder_and_postorder_traversal{
2+
3+
usingXunit;
4+
usingLeetCodeNet.Com_github_leetcode;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidConstructBinaryTree(){
9+
int[]inorder={9,3,15,20,7};
10+
int[]postorder={9,15,7,20,3};
11+
TreeNodeactual=newSolution().BuildTree(inorder,postorder);
12+
Assert.Equal("3,9,20,15,7",actual.ToString());
13+
}
14+
15+
[Fact]
16+
publicvoidConstructBinaryTree2(){
17+
int[]inorder={-1};
18+
int[]postorder={-1};
19+
TreeNodeactual=newSolution().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+
namespaceLeetCodeNet.G0101_0200.S0108_convert_sorted_array_to_binary_search_tree{
2+
3+
usingXunit;
4+
usingLeetCodeNet.Com_github_leetcode;
5+
6+
publicclassSolutionTest{
7+
[Fact]
8+
publicvoidSortedArrayToBST(){
9+
varactual=newSolution().SortedArrayToBST(newint[]{-10,-3,0,5,9});
10+
Assert.Equal("0,-10,null,-3,5,null,9",actual.ToString());
11+
}
12+
13+
[Fact]
14+
publicvoidSortedArrayToBST2(){
15+
varactual=newSolution().SortedArrayToBST(newint[]{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+
namespaceLeetCodeNet.G0101_0200.S0112_path_sum{
2+
3+
usingXunit;
4+
usingSystem.Collections.Generic;
5+
usingLeetCodeNet.Com_github_leetcode;
6+
7+
publicclassSolutionTest{
8+
[Fact]
9+
publicvoidHasPathSum(){
10+
varroot=TreeNode.Create(newList<int?>{5,4,8,11,null,13,4,7,2,null,null,null,1});
11+
Assert.True(newSolution().HasPathSum(root,22));
12+
}
13+
14+
[Fact]
15+
publicvoidHasPathSum2(){
16+
varroot=TreeNode.Create(newList<int?>{1,2,3});
17+
Assert.False(newSolution().HasPathSum(root,22));
18+
}
19+
20+
[Fact]
21+
publicvoidHasPathSum3(){
22+
Assert.False(newSolution().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+
namespaceLeetCodeNet.G0101_0200.S0117_populating_next_right_pointers_in_each_node_ii{
2+
3+
usingXunit;
4+
5+
publicclassSolutionTest{
6+
[Fact]
7+
publicvoidConnect(){
8+
Assert.Null(newSolution().Connect(null));
9+
}
10+
11+
[Fact]
12+
publicvoidConnect2(){
13+
varnode=newNode(
14+
1,
15+
newNode(2,newNode(4),newNode(5),null),
16+
newNode(3,null,newNode(7),null),
17+
null);
18+
19+
varnode7=newNode(7);
20+
varnode3=newNode(3,null,node7,null);
21+
varnode5=newNode(5,null,null,node7);
22+
varnode4=newNode(4,null,null,node5);
23+
varnode2=newNode(2,node4,node5,node3);
24+
varnode1=newNode(1,node2,node3,null);
25+
26+
Assert.Equal(node1.ToString(),newSolution().Connect(node).ToString());
27+
}
28+
29+
[Fact]
30+
publicvoidConnect3(){
31+
varnode=newNode(
32+
1,
33+
newNode(2,newNode(4,newNode(7),null,null),newNode(5),null),
34+
newNode(3,null,newNode(6,null,newNode(8),null),null),
35+
null);
36+
37+
varnode8=newNode(8,null,null,null);
38+
varnode7=newNode(7,null,null,node8);
39+
varnode6=newNode(6,null,node8,null);
40+
varnode3=newNode(3,null,node6,null);
41+
varnode5=newNode(5,null,null,node6);
42+
varnode4=newNode(4,node7,null,node5);
43+
varnode2=newNode(2,node4,node5,node3);
44+
varnode1=newNode(1,node2,node3,null);
45+
46+
Assert.Equal(node1.ToString(),newSolution().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+
namespaceLeetCodeNet.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+
usingSystem.Collections.Generic;
8+
usingLeetCodeNet.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+
publicclassSolution{
24+
publicIList<IList<int>>ZigzagLevelOrder(TreeNoderoot){
25+
varqueue=newQueue<TreeNode>();
26+
varresults=newList<IList<int>>();
27+
if(root==null){
28+
returnresults;
29+
}
30+
varlevel=newList<int>();
31+
queue.Enqueue(root);
32+
queue.Enqueue(null);
33+
vard=false;
34+
while(queue.Count>0){
35+
varc=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=newList<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+
returnresults;
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+
namespaceLeetCodeNet.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+
usingLeetCodeNet.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+
publicclassSolution{
22+
publicTreeNodeBuildTree(int[]inorder,int[]postorder){
23+
intinIndex=inorder.Length-1;
24+
intpostIndex=postorder.Length-1;
25+
returnHelper(inorder,postorder,refinIndex,refpostIndex,int.MaxValue);
26+
}
27+
28+
privateTreeNodeHelper(int[]inorder,int[]postorder,refintinIndex,refintpostIndex,inttarget){
29+
if(inIndex<0||inorder[inIndex]==target){
30+
returnnull;
31+
}
32+
TreeNoderoot=newTreeNode(postorder[postIndex--]);
33+
root.right=Helper(inorder,postorder,refinIndex,refpostIndex,(int)root.val);
34+
inIndex--;
35+
root.left=Helper(inorder,postorder,refinIndex,refpostIndex,target);
36+
returnroot;
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+
namespaceLeetCodeNet.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+
usingLeetCodeNet.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+
publicclassSolution{
23+
publicTreeNodeSortedArrayToBST(int[]nums){
24+
returnMakeTree(nums,0,nums.Length-1);
25+
}
26+
27+
privateTreeNodeMakeTree(int[]nums,intleft,intright){
28+
if(left>right){
29+
returnnull;
30+
}
31+
intmid=(left+right)/2;
32+
TreeNodemidNode=newTreeNode(nums[mid]);
33+
midNode.left=MakeTree(nums,left,mid-1);
34+
midNode.right=MakeTree(nums,mid+1,right);
35+
returnmidNode;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp