Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Check if binary tree is bst

Various possible way to check a given binary tree is bst or not.

1) Using inorder tree traversal.

2) Preorder tree traversal.

3) Postorder traversal.

We are know that binary search tree are contain two property.

1) Parent node of binary tree are greater to left child (left subtree).

2) Parent node of binary tree less then or equal to right child (right subtree).

Given a binary tree.

706080759020

Algorithm:

Methode 1 : Preorder


Method 2 : Using Inorder.


Method 3 : Postorder


c program to check if binary tree is bst.

Output

Code execution: view code execution process.

Stack Areamainroot(pointer)status (int) =1check_BSTroot(pointer)status(pointer)check_BSTroot(pointer)status(pointer)check_BSTroot(pointer)status(pointer)check_BSTroot(pointer) NULLstatus(pointer) Heap Areastruct Treedata (int)= 70left_child (pointer) right_child (pointer) struct Treedata (int)= 60left_child (pointer) right_child (pointer)= NULLstruct Treedata (int)= 80left_child (pointer) right_child (pointer) struct Treedata (int)= 75left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 90left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 20left_child (pointer)= NULLright_child (pointer)= NULL

Note that not given all step of execution process here. view more.

Try it yourself

Spread the post

Recommended Posts: