'''https://practice.geeksforgeeks.org/problems/check-for-bst/1
https://www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/
Check for BST 
Easy Accuracy: 21.58% Submissions: 100k+ Points: 2
Given the root of a binary tree. Check whether it is a BST or not.
Note: We are considering that BSTs can not contain duplicate Nodes.
A BST is defined as follows:

The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.
 

Example 1:

Input:
   2
 /    \
1      3
Output: 1 
Explanation: 
The left subtree of root node contains node 
with key lesser than the root node’s key and 
the right subtree of root node contains node 
with key greater than the root node’s key.
Hence, the tree is a BST.
Example 2:

Input:
  2
   \
    7
     \
      6
       \
        5
         \
          9
           \
            2
             \
              6
Output: 0 
Explanation: 
Since the node with value 7 has right subtree 
nodes with keys less than 7, this is not a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isBST() which takes the root of the tree as a parameter and returns true if the given binary tree is BST, else returns false. 

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the BST).

Constraints:
0 <= Number of edges <= 100000'''

# Python implementation to check if
# given Binary tree is a BST or not

# A binary tree node containing data
# field, left and right pointers


# Python3 program to check
# if a given tree is BST.
import math

# A binary tree node has data,
# pointer to left child and
# a pointer to right child


class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None


def isBSTUtil(root, prev):

    # traverse the tree in inorder fashion
    # and keep track of prev node
    if (root != None):
        if (isBSTUtil(root.left, prev) == True):
            return False

        # Allows only distinct valued nodes
        if (prev != None and
                root.data <= prev.data):
            return False

        prev = root
        return isBSTUtil(root.right, prev)

    return True


def isBST(root):
    prev = None
    return isBSTUtil(root, prev)


# Driver Code
if __name__ == '__main__':
    root = Node(3)
    root.left = Node(2)
    root.right = Node(5)
    root.right.left = Node(1)
    root.right.right = Node(4)
    #root.right.left.left = Node(40)

    if (isBST(root) == None):
        print("Is BST")
    else:
        print("Not a BST")

'''https://practice.geeksforgeeks.org/problems/check-for-bst/1
https://www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/
Check for BST 
Easy Accuracy: 21.58% Submissions: 100k+ Points: 2
Given the root of a binary tree. Check whether it is a BST or not.
Note: We are considering that BSTs can not contain duplicate Nodes.
A BST is defined as follows:

The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.
 

Example 1:

Input:
   2
 /    \
1      3
Output: 1 
Explanation: 
The left subtree of root node contains node 
with key lesser than the root node’s key and 
the right subtree of root node contains node 
with key greater than the root node’s key.
Hence, the tree is a BST.
Example 2:

Input:
  2
   \
    7
     \
      6
       \
        5
         \
          9
           \
            2
             \
              6
Output: 0 
Explanation: 
Since the node with value 7 has right subtree 
nodes with keys less than 7, this is not a BST.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isBST() which takes the root of the tree as a parameter and returns true if the given binary tree is BST, else returns false. 

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the BST).

Constraints:
0 <= Number of edges <= 100000'''

# Python implementation to check if
# given Binary tree is a BST or not

# A binary tree node containing data
# field, left and right pointers


# Python3 program to check
# if a given tree is BST.
import math

# A binary tree node has data,
# pointer to left child and
# a pointer to right child


class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None


def isBSTUtil(root, prev):

    # traverse the tree in inorder fashion
    # and keep track of prev node
    if (root != None):
        if (isBSTUtil(root.left, prev) == True):
            return False

        # Allows only distinct valued nodes
        if (prev != None and
                root.data <= prev.data):
            return False

        prev = root
        return isBSTUtil(root.right, prev)

    return True


def isBST(root):
    prev = None
    return isBSTUtil(root, prev)


# Driver Code
if __name__ == '__main__':
    root = Node(3)
    root.left = Node(2)
    root.right = Node(5)
    root.right.left = Node(1)
    root.right.right = Node(4)
    #root.right.left.left = Node(40)

    if (isBST(root) == None):
        print("Is BST")
    else:
        print("Not a BST")

https://practice.geeksforgeeks.org/problems/check-for-bst/1 https://www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/ Check for BST  Easy Accuracy: 21.58% Submissions: 100k+ Points: 2 Given the root of a binary tree. Check whether it is a BST or not. Note: We are considering that BSTs can not contain duplicate Nodes. A BST is defined as follows:  The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees.    Example 1:  Input:    2  /    \ 1      3 Output: 1  Explanation:  The left subtree of root node contains node  with key lesser than the root node’s key and  the right subtree of root node contains node  with key greater than the root node’s key. Hence, the tree is a BST. Example 2:  Input:   2    \     7      \       6        \         5          \           9            \             2              \               6 Output: 0  Explanation:  Since the node with value 7 has right subtree  nodes with keys less than 7, this is not a BST. Your Task: You don't need to read input or print anything. Your task is to complete the function isBST() which takes the root of the tree as a parameter and returns true if the given binary tree is BST, else returns false.   Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST).  Constraints: 0 <= Number of edges <= 100000 # Python implementation to check if # given Binary tree is a BST or not # A binary tree node containing data # field, left and right pointers # Python3 program to check # if a given tree is BST. # A binary tree node has data, # pointer to left child and # a pointer to right child # traverse the tree in inorder fashion # and keep track of prev node # Allows only distinct valued nodes # Driver Code #root.right.left.left = Node(40)