-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsArrayBST.cs
More file actions
56 lines (42 loc) · 977 Bytes
/
Copy pathIsArrayBST.cs
File metadata and controls
56 lines (42 loc) · 977 Bytes
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
using System;
// To execute C#, please define "static void Main" on a class
// named Solution.
class Solution
{
static void Main(string[] args)
{
}
}
public class Node
{
public int Data {get; set;}
public Node Left {get; set;}
public Node Right {get; set;}
public Node(int newdata)
{
Data = newdata;
Left = Right = null;
}
}
public class BinaryTree
{
public Node Root {get; set;}
public BinaryTree()
{
Root = null;
}
public bool IsBST()
{
return IsBSTUtil(Root, Int32.MinValue, Int32.MaxValue);
}
private bool IsBSTUtil(Node node, int min, int max)
{
if(node == null)
return true;
if((node.Data < min) || (node.Data > max))
{
return false;
}
return (IsBSTUtil(node, min, node.Data -1) && IsBSTUtil(node.Right, node.Data +1, max));
}
}