Day i do not know
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_701._Insert_into_a_Binary_Search_Tree</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
9
701. Insert into a Binary Search Tree/Program.cs
Normal file
9
701. Insert into a Binary Search Tree/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _701._Insert_into_a_Binary_Search_Tree;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
34
701. Insert into a Binary Search Tree/Solution.cs
Normal file
34
701. Insert into a Binary Search Tree/Solution.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
namespace _701._Insert_into_a_Binary_Search_Tree;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public TreeNode InsertIntoBST(TreeNode root, int val)
|
||||
{
|
||||
if(root == null)
|
||||
return new TreeNode(val);
|
||||
TreeNode cur = root;
|
||||
while(true)
|
||||
{
|
||||
int curVal = cur.val;
|
||||
if(val < curVal)
|
||||
{
|
||||
if (cur.left == null)
|
||||
{
|
||||
cur.left = new TreeNode(val);
|
||||
break;
|
||||
}
|
||||
cur = cur.left;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cur.right == null)
|
||||
{
|
||||
cur.right = new TreeNode(val);
|
||||
break;
|
||||
}
|
||||
cur = cur.right;
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
||||
15
701. Insert into a Binary Search Tree/TreeNode.cs
Normal file
15
701. Insert into a Binary Search Tree/TreeNode.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace _701._Insert_into_a_Binary_Search_Tree;
|
||||
|
||||
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user