Pergunta de entrevista da empresa Meta

LCA problem with no parent pointers. Given the root of a tree and pointers to two nodes contained in that tree, return the lowest common ancestor of the two nodes. IE, the common ancestor furthest from the root.

Respostas da entrevista

Sigiloso

26 de abr. de 2014

Was the data structure a binary tree or a binary search tree?

Sigiloso

4 de mar. de 2016

bool printCommonAncestor(struct node* node,int x,int y) { if (node == NULL) return false; // first recur on left subtree bool left = printCommonAncestor(node->left,x,y); bool right = printCommonAncestor(node->right,x,y); if(left && right) printf("%d ", node->data); if(left || right) { if(node->data == x || node->data==y) { printf("%d ", node->data); } return true; } if(node->data == x || node->data==y) return true; return false; // now deal with the node } Do a Post order traversal. If its on left and right of the tree then return that node

Sigiloso

4 de mar. de 2016

Array of 256 chars for each string. Assign to 0. Then as a character comes increment the count. Compute a hash key using these 256 values and store the hash key->String mapping. Do so for each string, if the mapping already exists, add this string to the list of strings for this mapping.