-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-search-tree-is-valid.linq
70 lines (62 loc) · 1.21 KB
/
binary-search-tree-is-valid.linq
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<Query Kind="Program" />
void Main()
{
// create a binary tree
Node binaryTree = CreateNode(10,
CreateNode(5, CreateNode(3), CreateNode(7)),
CreateNode(15, CreateNode(13, Right: CreateNode(14)), CreateNode(17)
));
// search for node that is not valid BST node
bool isBinarySearchTree = !DFS(binaryTree, (n) =>
{
bool rc = false;
Console.WriteLine(n.Value);
if(n.Left != null && n.Value > n.Left.Value)
{
rc = true;
}
else if (n.Right != null && n.Value < n.Right.Value)
{
rc = true;
}
return rc;
});
Console.WriteLine("Is Binary Search Tree? {0}", isBinarySearchTree);
}
// given
public class Node
{
public int Value { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
}
// Helper code
Node CreateNode(int Value, Node Left = null, Node Right = null)
{
return new Node { Value = Value, Left = Left, Right = Right };
}
bool DFS(Node root, Func<Node, bool> visit)
{
if (root == null)
{
return true;
}
bool rc = false;
if(visit(root) ||
DFS(root.Left, visit) ||
DFS(root.Right, visit))
{
rc = true;
}
return rc;
}
void DFS(Node root, Action<Node> visit)
{
if(root == null)
{
return;
}
visit(root);
DFS(root.Left, visit);
DFS(root.Right, visit);
}