Inorder tree tranversal on binary search tree doesn't give the elements in order?
-
28-09-2020 - |
Question
I have been told that inorder tree tranversal of binary search trees returns the tree elements in order. I came up with this binary search tree:
20
/
10
\
26
However the inorder tree tranversal of this seems to be 10, 26, 20?! What is wrong here?
Solution
This is not a valid binary search tree, since 26 is greater than 20 and is in its left subtree.
OTHER TIPS
That is not correct BST as Narek mentioned. BST should be -
20
/ \
10 26
For inorder traversal, start visiting tree from Top to bottom and left to right manner and print a node only when it is referenced 2nd time.
So, inorder traversal here would be 10,20,26.
Licensed under: CC-BY-SA with attribution
Not affiliated with cs.stackexchange