Pergunta de entrevista da empresa Meta

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

Respostas da entrevista

Sigiloso

11 de fev. de 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

Sigiloso

24 de set. de 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

Sigiloso

5 de dez. de 2011

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

Sigiloso

22 de nov. de 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.

Sigiloso

16 de dez. de 2010

traverse each tree into an array, sort them, compare