GeeksforGeeks » Interview Questions
Amazon Interview Question for Software Engineer/Developer about Trees
(5 posts)-
you have two BST , they contain the same amount of elements "N" but there structure is different. how will you cross check that the trees are identical with complexity O(N). Also how will you do the same in case its only Binary tree and not a Binary search tree.
-
If their structure is not the same, then in what sense are they identical?
-
The two BSTs have the same number of element and same element, only structure is different.
For example, following two BSTs
4 5 / \ / 3 5 4 / 3 -
inordr traversal in case of BST.
-
inorder traversal in BST case..
we can use hashMap for binary tree , so that it work for O(n) complexity.in case of binary tree...add each node in hash table and increment count if elements are repeated...
now do the same with another tree...increment count if it already exists.in the end you just need to check that all hashed nodes has even count..if odd count found..
they are not identical
Reply
You must log in to post.