public Node(int data)
 {
     left = new Node();
     right = new Node();
     parent = new Node();
     this.data = data;
 }
 public static bool CheckMinMax(Node root, int min, int max)
 {
     if (root == null)
         return true;
     if (root.data < min || root.data > max)
         return false;
     bool checkLeft = CheckMinMax(root.left, min, root.data);
     bool checkRight = CheckMinMax(root.right, root.data, max);
     return checkLeft && checkRight;
 }
 public static int CheckHeight(Node root)
 {
     if (root == null)
         return 0;
     int leftHeight = CheckHeight(root.left);
     int rightHeight = CheckHeight(root.right);
     if (Math.Abs(leftHeight - rightHeight) >= 1)
         return -1;
     return Math.Max(leftHeight, rightHeight) + 1;
 }
 public Node CreateMinBST(int[] array, int start, int end)
 {
     if (end < start)
         return null;
     int middle = (start + end) / 2;
     Node mNode = new Node(array[middle]);
     Node left = CreateMinBST(array, start, middle - 1);
     Node right = CreateMinBST(array, middle + 1, end);
     mNode.left = left;
     mNode.right = right;
     return mNode;
 }
 //Not tested.
 public Node CommonAnce(Node root, Node first, Node second)
 {
     if (root == null)
         return null;
     if (root == first)
         return first;
     if (root == second)
         return second;
     Node left = CommonAnce(root.left, first, second);
     Node right = CommonAnce(root.right, first, second);
     if (left == null)
         return right;
     else if (right == null)
         return left;
     return root;
 }
 public static Node NextNode(Node n)
 {
     Node next;
     if (n.right != null)
     {
         next = n.right;
         while (next.left != null)
             next = next.left;
         return next;
     }
     next = n.parent;
     Node current = n;
     while (next != null && next.right == n)
     {
         next = next.parent;
         n = n.parent;
     }
     if (next == null)
         return null;
     return next;
 }
 List<List<Node>> CreateLevelLinkedList(Node root)
 {
     List<List<Node>> result = new List<List<Node>>();
     Queue<Node> levelQ = new Queue<Node>();
     Queue<Node> nextQ = new Queue<Node>();
     levelQ.Enqueue(root);
     result.Add(new List<Node>());
     while (levelQ.Count > 0)
     {
         Node current = levelQ.Dequeue();
         result.Last().Add(current);
         if (current.left != null)
             nextQ.Enqueue(current.left);
         if (current.right != null)
             nextQ.Enqueue(current.right);
         if (levelQ.Count == 0)
         {
             levelQ = nextQ;
             result.Add(new List<Node>());
         }
     }
     return result;
 }
 public static bool IsBST(Node root)
 {
     return CheckMinMax(root, Int32.MinValue, Int32.MaxValue);
 }