This can be done in many ways . The simplest way is by using the Inorder Traversal . If we traverse a binary search tree using inorder traversal all the elements are in sorted order .
For the above binary search tree Inorder traversal is
1 2 3 4 5 6 7
In the following code flag is used like a boolean varible . In the Inorder traversal if the elements are not in sorted order flag is set to 0 . The variable 'min' is used to store the previous value in inorder traversal . Initially it is assigned to the mininum integer value possible which is -32768.
Code :(in C)
Please let me know if you have any questions .
No comments:
Post a Comment