I was studying this https://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/ but could not find a way to construct a binary tree using preorder and inorder with duplicate values because hashmaps and linear search does not work in some cases to find the right index. Please tell if there is an algorithm for this. Any kind of help would be appreciated.
-
1Probably you can add to a set that tracks already seen indices in case of duplicate values.SomeDude– SomeDude2021-06-23 22:51:34 +00:00Commented Jun 23, 2021 at 22:51
-
3If all the values are the same, then the inorder and preorder traversals are the same no matter what the shape of the tree is, so the shape cannot be reconstructed.Matt Timmermans– Matt Timmermans2021-06-24 02:49:51 +00:00Commented Jun 24, 2021 at 2:49
-
@MattTimmermans I feel bad but you are right. Its not possible.Gaurav– Gaurav2021-06-24 05:25:22 +00:00Commented Jun 24, 2021 at 5:25
Add a comment
|