This commit is contained in:
Electrominch
2022-10-12 02:30:23 +03:00
parent 02afed9c4c
commit 01c5720116
39 changed files with 517 additions and 30 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_116._Populating_Next_Right_Pointers_in_Each_Node</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,25 @@
namespace _116._Populating_Next_Right_Pointers_in_Each_Node;
// Definition for a Node.
public class Node
{
public int val;
public Node? left;
public Node? right;
public Node? next;
public Node() { }
public Node(int _val)
{
val = _val;
}
public Node(int _val, Node? _left, Node? _right, Node? _next)
{
val = _val;
left = _left;
right = _right;
next = _next;
}
}

View File

@@ -0,0 +1,9 @@
namespace _116._Populating_Next_Right_Pointers_in_Each_Node;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}

View File

@@ -0,0 +1,31 @@
namespace _116._Populating_Next_Right_Pointers_in_Each_Node;
public class Solution
{
public Node? Connect(Node? root)
{
if (root == null)
return null;
List<Node> curLevel = new List<Node>() { root };
List<Node> nextLevel = new List<Node>();
while (curLevel.Count > 0)
{
for (int i = 0; i < curLevel.Count - 1; i++)
curLevel[i].next = curLevel[i + 1];
nextLevel.Clear();
foreach (var n in curLevel)
{
if (n == null)
continue;
if (n.left != null)
nextLevel.Add(n.left);
if (n.right != null)
nextLevel.Add(n.right);
}
List<Node> buf = curLevel;
curLevel = nextLevel;
nextLevel = buf;
}
return root;
}
}

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_144._Binary_Tree_Preorder_Traversal</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,9 @@
namespace _144._Binary_Tree_Preorder_Traversal;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}

View File

@@ -0,0 +1,22 @@
namespace _144._Binary_Tree_Preorder_Traversal;
public class Solution
{
public IList<int> PreorderTraversal(TreeNode root)
{
List<int> res = new List<int>();
Stack<TreeNode> stack = new Stack<TreeNode>();
if (root != null)
stack.Push(root);
while (stack.Count > 0)
{
TreeNode cur = stack.Pop();
if (cur.right != null)
stack.Push(cur.right);
if (cur.left != null)
stack.Push(cur.left);
res.Add(cur.val);
}
return res;
}
}

View File

@@ -0,0 +1,16 @@
namespace _144._Binary_Tree_Preorder_Traversal;
//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;
}
}

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_145._Binary_Tree_Postorder_Traversal</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,9 @@
namespace _145._Binary_Tree_Postorder_Traversal;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}

View File

@@ -0,0 +1,24 @@
namespace _145._Binary_Tree_Postorder_Traversal;
public class Solution
{
public IList<int> PostorderTraversal(TreeNode root)
{
var result = new List<int>();
if (root == null) return result;
var stack = new Stack<TreeNode>();
stack.Push(root);
while (stack.Any())
{
var cur = stack.Pop();
result.Add(cur.val);
if (cur.left != null)
stack.Push(cur.left);
if (cur.right != null)
stack.Push(cur.right);
}
result.Reverse();
return result;
}
}

View File

@@ -0,0 +1,16 @@
namespace _145._Binary_Tree_Postorder_Traversal;
//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;
}
}

View File

@@ -1,6 +1,4 @@
using System.ComponentModel; namespace _200._Number_of_Islands;
namespace _200._Number_of_Islands;
public class Solution public class Solution
{ {

View File

@@ -7,6 +7,6 @@ internal class Program
TreeNode root = new TreeNode(6); TreeNode root = new TreeNode(6);
root.left = new TreeNode(2) { left = new TreeNode(0), right = new TreeNode(4) { left = new TreeNode(3), right = new TreeNode(5) } }; root.left = new TreeNode(2) { left = new TreeNode(0), right = new TreeNode(4) { left = new TreeNode(3), right = new TreeNode(5) } };
root.right = new TreeNode(8) { left = new TreeNode(7), right = new TreeNode(9) }; root.right = new TreeNode(8) { left = new TreeNode(7), right = new TreeNode(9) };
Console.WriteLine(new Solution().LowestCommonAncestor(root, new TreeNode(3), new TreeNode(5)).val); Console.WriteLine(new Solution().LowestCommonAncestor(root, new TreeNode(3), new TreeNode(5))?.val);
} }
} }

View File

@@ -1,7 +1,8 @@
namespace _235._Lowest_Common_Ancestor_of_a_Binary_Search_Tree; namespace _235._Lowest_Common_Ancestor_of_a_Binary_Search_Tree;
//Definition for a binary tree node. //Definition for a binary tree node.
public class TreeNode { public class TreeNode
{
public int val; public int val;
public TreeNode? left; public TreeNode? left;
public TreeNode? right; public TreeNode? right;

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_509._Fibonacci_Number</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,8 @@
namespace _509._Fibonacci_Number;
internal class Program
{
static void Main(string[] args)
{
}
}

View File

@@ -0,0 +1,20 @@
namespace _509._Fibonacci_Number;
public class Solution
{
public int Fib(int n)
{
if (n == 0)
return 0;
n--;
int cur0 = 0;
int cur1 = 1;
while (--n > 0)
{
int buf = cur0 + cur1;
cur0 = cur1;
cur1 = buf;
}
return cur0 + cur1;
}
}

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_617._Merge_Two_Binary_Trees</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,58 @@
namespace _617._Merge_Two_Binary_Trees;
internal class Program
{
static void Main(string[] args)
{
TreeNode root1 = new TreeNode(1);
root1.left = new TreeNode(3) { left = new TreeNode(5) };
root1.right = new TreeNode(2);
TreeNode root2 = new TreeNode(2);
root2.left = new TreeNode(1) { right = new TreeNode(4) };
root2.right = new TreeNode(3) { right = new TreeNode(7) };
var res = new Write().LevelOrder(new Solution().MergeTrees(root1, root2)!);
Console.WriteLine($"\nOutput");
foreach (var row in res)
Console.WriteLine(String.Join(" ", row));
}
public class Write
{
public IList<IList<int>> LevelOrder(TreeNode root)
{
List<IList<int>> output = new List<IList<int>>();
RecursiveTraversal(output, new List<TreeNode>() { root });
return output;
}
private void RecursiveTraversal(List<IList<int>> output, List<TreeNode> curLevel)
{
if (curLevel.Count == 0)
return;
List<int> levelValues = new List<int>();
foreach (var n in curLevel)
{
if (n == null)
continue;
levelValues.Add(n.val);
}
if (levelValues.Count == 0)
return;
output.Add(levelValues);
List<TreeNode> nextLevel = new List<TreeNode>();
foreach (var n in curLevel)
{
if (n == null)
continue;
if (n.left != null)
nextLevel.Add(n.left);
if (n.right != null)
nextLevel.Add(n.right);
}
RecursiveTraversal(output, nextLevel);
}
}
}

View File

@@ -0,0 +1,31 @@
namespace _617._Merge_Two_Binary_Trees;
public class Solution
{
public TreeNode? MergeTrees(TreeNode root1, TreeNode root2)
{
if (root1 == null && root2 == null)
return null;
else if (root1 == null)
return root2;
else if (root2 == null)
return root1;
var res = new TreeNode();
RecursionMerge(res, root1, root2);
return res;
}
private void RecursionMerge(TreeNode cur, TreeNode? t1, TreeNode? t2)
{
if (t1 != null)
cur.val += t1.val;
if (t2 != null)
cur.val += t2.val;
cur.left = (t1?.left != null || t2?.left != null) ? new TreeNode() : null;
cur.right = (t1?.right != null || t2?.right != null) ? new TreeNode() : null;
if (cur.left != null)
RecursionMerge(cur.left, t1?.left, t2?.left);
if (cur.right != null)
RecursionMerge(cur.right, t1?.right, t2?.right);
}
}

View File

@@ -0,0 +1,16 @@
namespace _617._Merge_Two_Binary_Trees;
//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;
}
}

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_70._Climbing_Stairs</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,9 @@
namespace _70._Climbing_Stairs;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}

View File

@@ -0,0 +1,19 @@
namespace _70._Climbing_Stairs;
public class Solution
{
public int ClimbStairs(int n)
{
if (n == 0)
return 0;
int cur0 = 0;
int cur1 = 1;
while (--n > 0)
{
int buf = cur0 + cur1;
cur0 = cur1;
cur1 = buf;
}
return cur0 + cur1;
}
}

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_94._Binary_Tree_Inorder_Traversal</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,9 @@
namespace _94._Binary_Tree_Inorder_Traversal;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}

View File

@@ -0,0 +1,23 @@
namespace _94._Binary_Tree_Inorder_Traversal;
public class Solution
{
public List<int> inorderTraversal(TreeNode root)
{
List<int> res = new List<int>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode? curr = root;
while (curr != null || stack.Count > 0)
{
while (curr != null)
{
stack.Push(curr);
curr = curr.left;
}
curr = stack.Pop();
res.Add(curr.val);
curr = curr.right!;
}
return res;
}
}

View File

@@ -0,0 +1,15 @@
namespace _94._Binary_Tree_Inorder_Traversal;
//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;
}
}

View File

@@ -95,7 +95,21 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "98. Validate Binary Search
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "235. Lowest Common Ancestor of a Binary Search Tree", "235. Lowest Common Ancestor of a Binary Search Tree\235. Lowest Common Ancestor of a Binary Search Tree.csproj", "{B4466695-F56D-40E6-8D9C-D5955ACE8B4E}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "235. Lowest Common Ancestor of a Binary Search Tree", "235. Lowest Common Ancestor of a Binary Search Tree\235. Lowest Common Ancestor of a Binary Search Tree.csproj", "{B4466695-F56D-40E6-8D9C-D5955ACE8B4E}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "200. Number of Islands", "200. Number of Islands\200. Number of Islands.csproj", "{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "200. Number of Islands", "200. Number of Islands\200. Number of Islands.csproj", "{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "617. Merge Two Binary Trees", "617. Merge Two Binary Trees\617. Merge Two Binary Trees.csproj", "{79FEA33C-30E7-4FE6-A7C8-18BD13EE1B9A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "116. Populating Next Right Pointers in Each Node", "116. Populating Next Right Pointers in Each Node\116. Populating Next Right Pointers in Each Node.csproj", "{DD19137E-1A2F-4803-910B-A63826F65159}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "144. Binary Tree Preorder Traversal", "144. Binary Tree Preorder Traversal\144. Binary Tree Preorder Traversal.csproj", "{0DA14E84-9F56-4384-9743-BA7FD7539113}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "94. Binary Tree Inorder Traversal", "94. Binary Tree Inorder Traversal\94. Binary Tree Inorder Traversal.csproj", "{60E8258E-4BBC-42C7-96CD-C67DB629E0E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "145. Binary Tree Postorder Traversal", "145. Binary Tree Postorder Traversal\145. Binary Tree Postorder Traversal.csproj", "{6E954C2F-608C-4294-91CF-62B8E49CC415}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "509. Fibonacci Number", "509. Fibonacci Number\509. Fibonacci Number.csproj", "{73450FFA-253B-42CC-B5E7-8769CFF1BE96}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "70. Climbing Stairs", "70. Climbing Stairs\70. Climbing Stairs.csproj", "{625340C0-FC67-4555-B49F-FB28E3177F72}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -291,6 +305,34 @@ Global
{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Debug|Any CPU.Build.0 = Debug|Any CPU {02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Release|Any CPU.ActiveCfg = Release|Any CPU {02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Release|Any CPU.Build.0 = Release|Any CPU {02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Release|Any CPU.Build.0 = Release|Any CPU
{79FEA33C-30E7-4FE6-A7C8-18BD13EE1B9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79FEA33C-30E7-4FE6-A7C8-18BD13EE1B9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79FEA33C-30E7-4FE6-A7C8-18BD13EE1B9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79FEA33C-30E7-4FE6-A7C8-18BD13EE1B9A}.Release|Any CPU.Build.0 = Release|Any CPU
{DD19137E-1A2F-4803-910B-A63826F65159}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD19137E-1A2F-4803-910B-A63826F65159}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD19137E-1A2F-4803-910B-A63826F65159}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD19137E-1A2F-4803-910B-A63826F65159}.Release|Any CPU.Build.0 = Release|Any CPU
{0DA14E84-9F56-4384-9743-BA7FD7539113}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0DA14E84-9F56-4384-9743-BA7FD7539113}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0DA14E84-9F56-4384-9743-BA7FD7539113}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0DA14E84-9F56-4384-9743-BA7FD7539113}.Release|Any CPU.Build.0 = Release|Any CPU
{60E8258E-4BBC-42C7-96CD-C67DB629E0E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60E8258E-4BBC-42C7-96CD-C67DB629E0E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60E8258E-4BBC-42C7-96CD-C67DB629E0E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60E8258E-4BBC-42C7-96CD-C67DB629E0E1}.Release|Any CPU.Build.0 = Release|Any CPU
{6E954C2F-608C-4294-91CF-62B8E49CC415}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E954C2F-608C-4294-91CF-62B8E49CC415}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E954C2F-608C-4294-91CF-62B8E49CC415}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E954C2F-608C-4294-91CF-62B8E49CC415}.Release|Any CPU.Build.0 = Release|Any CPU
{73450FFA-253B-42CC-B5E7-8769CFF1BE96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73450FFA-253B-42CC-B5E7-8769CFF1BE96}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73450FFA-253B-42CC-B5E7-8769CFF1BE96}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73450FFA-253B-42CC-B5E7-8769CFF1BE96}.Release|Any CPU.Build.0 = Release|Any CPU
{625340C0-FC67-4555-B49F-FB28E3177F72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{625340C0-FC67-4555-B49F-FB28E3177F72}.Debug|Any CPU.Build.0 = Debug|Any CPU
{625340C0-FC67-4555-B49F-FB28E3177F72}.Release|Any CPU.ActiveCfg = Release|Any CPU
{625340C0-FC67-4555-B49F-FB28E3177F72}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE