-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
42 lines (38 loc) · 1.04 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
namespace InsertIntoABinarySearchTree
{
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
class Program
{
static void Main(string[] args)
{
TreeNode a = new TreeNode(4);
TreeNode b = new TreeNode(2);
TreeNode c = new TreeNode(7);
TreeNode d = new TreeNode(1);
TreeNode e = new TreeNode(3);
a.left = b;
a.right = c;
b.left = d;
b.right = e;
int val = 5;
Console.WriteLine(InsertIntoBST(a, val));
}
static TreeNode InsertIntoBST(TreeNode root, int val)
{
if (root == null)
return new TreeNode(val);
if (val > root.val)
root.right = InsertIntoBST(root.right, val);
else
root.left = InsertIntoBST(root.left, val);
return root;
}
}
}