Domanda di colloquio di Meta

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

Risposte di colloquio

Anonimo

11 feb 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

Anonimo

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

Anonimo

5 dic 2011

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

Anonimo

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.

Anonimo

16 dic 2010

traverse each tree into an array, sort them, compare