BST TRAVERSAL

 Ways to traverse in a bst.


1. Preorder Traversal - 

        Use root left right technique, i.e. first visit root then visit left sub tree and then visit right subtree.

        First time it may look confusing but its fairly simple a look on code snippet below.

        Root is the starting node and array holds the preorder of the array.

2. Inorder Traversal

        Use left root right technique, i.e. first visit left sub tree then visit root and then visit right subtree.


3.  Postorder Traversal

         
Use left right root technique, i.e. first visit left sub tree then visit right subtree and  then visit root .



time - O(n) : because we will be visiting all the nodes in the tree.
space - O(n) : space required by array to store the order of bst. 


Code link - traversal in bst

Please leave comment for any suggestions or topic you would like to read.

Comments