CHECK IF GIVEN BINARY TREE IS A BST/ VALIDATE BST
Question link : validate bst practise
Approach -
Take variable maxRange assign it as big positive number, minRange assign it as big negative number.When we move to left sub tree then our maxRange will be tree.value as per the properties of bst.
Similarly as we move towards right sub tree our min range will be tree.value.
If at any point property of bst fails we will return false.
If both left sub tree and right sub tree follows the property then we it means that given binary tree is a bst.
time - O(n) : as we need to move to all nodes in a tree.
space - O(d) : Auxillary stack space needed by recursion where d is the depth of tree.
Solution - validate bst solution
Please leave comment for any suggestions or topic you would like to read.


Very Helpfull..Thank you so much
ReplyDeleteNice to hear from you that it helped.
Delete