using
System;
using
System.Collections.Generic;
class
TreeNode {
public
int
val;
public
TreeNode left, right;
}
class
GFG {
static
TreeNode NewNode(
int
data)
{
TreeNode temp =
new
TreeNode();
temp.val = data;
temp.left =
null
;
temp.right =
null
;
return
temp;
}
static
TreeNode Insert(TreeNode root,
int
val)
{
TreeNode newnode = NewNode(val);
TreeNode x = root;
TreeNode y =
null
;
while
(x !=
null
) {
y = x;
if
(val < x.val) {
x = x.left;
}
else
{
x = x.right;
}
}
if
(y ==
null
) {
y = newnode;
}
else
if
(val < y.val) {
y.left = newnode;
}
else
{
y.right = newnode;
}
return
y;
}
static
void
Inorder(List<
int
> inord, TreeNode root)
{
if
(root ==
null
) {
return
;
}
Inorder(inord, root.left);
inord.Add(root.val);
Inorder(inord, root.right);
}
static
bool
BSTSearch(List<
int
> inord,
int
target)
{
return
inord.Contains(target);
}
static
void
PrintArr(
int
N,
int
[] targets,
List<
int
> inorder)
{
for
(
int
i = 0; i < N; i++) {
if
(BSTSearch(inorder, targets[i])) {
Console.WriteLine(
"Found"
);
}
else
{
Console.WriteLine(
"Not Found"
);
}
}
}
static
void
Main(
string
[] args)
{
TreeNode root =
null
;
root = Insert(root, 8);
Insert(root, 15);
Insert(root, 4);
Insert(root, 7);
Insert(root, 1);
Insert(root, 6);
Insert(root, 5);
List<
int
> inor =
new
List<
int
>();
Inorder(inor, root);
int
[] targets = { 6, 2, 7, 4, 5, 1, 3 };
int
N = targets.Length;
PrintArr(N, targets, inor);
}
}