Determine if given Binary Tree is a BST or not

Given a Binary Tree, determine if it is a BST or not.

This problem has a simple recursive solution. The BST property “every node on the right subtree has to be larger than the current node and every node on the left subtree has to be smaller than the current node” is the key to figuring out whether a tree is a BST or not.

The greedy algorithm – simply traverse the tree, at every node check whether the node contains a value larger than the value at the left child and smaller than the value on the right child – does not work for all cases. Consider the following tree:
20
/ \
10 30
/ \
5 40

In the tree above, each node meets the condition that the node contains a value larger than its left child and smaller than its right child hold, and yet it is not a BST: the value 5 is on the right subtree of the node containing 20, a violation of the BST property.

Instead of making a decision based solely on the values of a node and its children, we also need information flowing down from the parent as well. In the case of the tree above, if we could remember about the node containing the value 20, we would see that the node with value 5 is violating the BST property contract.

So the condition we need to check at each node is:

if the node is the left child of its parent, then it must be smaller than (or equal to) the parent and it must pass down the value from its parent to its right subtree to make sure none of the nodes in that subtree is greater than the parent.

if the node is the right child of its parent, then it must be larger than the parent and it must pass down the value from its parent to its left subtree to make sure none of the nodes in that subtree is lesser than the parent.

Below is C++/Java implementation of the idea:

C++

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

71

72

73

74

75

76

77

78

79

80

81

#include <iostream>

#include <climits>

using namespacestd;

// Data structure to store a Binary Search Tree node

structNode

{

intdata;

Node *left,*right;

};

// Function to create a new binary tree node having given key

Node*newNode(intkey)

{

Node*node=newNode;

node->data=key;

node->left=node->right=nullptr;

returnnode;

}

// Recursive function to insert an key into BST

Node*insert(Node*root,intkey)

{

// if the root is null, create a new node an return it

if(root==nullptr)

returnnewNode(key);

// if given key is less than the root node, recurse for left subtree

if(key<root->data)

root->left=insert(root->left,key);

// if given key is more than the root node, recurse for right subtree

else

root->right=insert(root->right,key);

returnroot;

}

// Function to determine if given Binary Tree is a BST or not by keeping a

The time complexity of above solution is O(n) and auxiliary space used by the program is O(n).

Another approach –

We know that an in-order traversal of a binary search tree returns the nodes in sorted order. Thus, to determine if given binary tree is BST or not, we can perform in-order traversal and keep track of the last visited node while traversing the tree and check whether its key is smaller (or smaller/equal, if duplicates are to be allowed in the tree) compared to the current key.

Thanks a lot for bringing this issue to our notice. There was problem with the Java implementation where prev node was passed by value. We have fixed the issue by changing the data of prev node instead of updating the reference.