Question d’entretien chez Meta

Given two binary trees, return true if they have same elements (irrespective of tree structure)

Réponses aux questions d'entretien

Utilisateur anonyme

11 févr. 2011

traverse 1st tree and add all node data into hash table of traverse 2nd tree and foreach node: if data not in hashtable return false; otherwise, decrease count by 1, remove the key if count is 0 return hashtable.ItemCount == 0;

3

Utilisateur anonyme

24 sept. 2010

add all elements of one tree to a hashtable for the other tree, add them to the same hashtable and if it's empty, return false, and if there is a collision, erase the entry from the table if nothing's left in the table after, we're good

2

Utilisateur anonyme

5 déc. 2011

If the trees are binary search trees, it is possible to compare them quickly by calling repeatedly getSuccessor();

Utilisateur anonyme

22 nov. 2010

The above algorithm is totally wrong. Let's take a look at this counter example: Tree A: 1 2 2 Tree B: 1 1 2 Your algorithm will return "we are good" in this case.

Utilisateur anonyme

16 déc. 2010

traverse each tree into an array, sort them, compare