GeeksforGeeks » Interview Questions

Amazon Interview Question for Software Engineer/Developer about Trees

(5 posts)

Tags:

  1. geek4u
    Member
    Posted 3 months ago #

    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.

  2. slvrhwk
    Member
    Posted 3 months ago #

    If their structure is not the same, then in what sense are they identical?

  3. geek4u
    Member
    Posted 3 months ago #

    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
    
  4. google
    Member
    Posted 3 months ago #

    inordr traversal in case of BST.

  5. atul007
    Member
    Posted 3 months ago #

    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.

RSS feed for this topic