Pergunta de entrevista da empresa LinkedIn

Find whether a given binary tree is image of the other one

Respostas da entrevista

Sigiloso

20 de set. de 2015

Hope you meant "image" => "clone/replica". Not "Mirror". Here is my take (below code will compare whether two BT are same): // This function will return true if both binary trees are same. Else return false. boolean compare (Node root1, Node root2) { if(root1 == root2) return true; // both are null or both points to same reference, then return true. if( (root1 == null && root2 != null) || (root1 != null && root2 == null) || root1.data != root2.data) return false; // One of them is partial. return compare(root1.left, root2.left) && compare(root1.right, root2.right); }

Sigiloso

25 de out. de 2015

{{{ bool isM (node * t1, node * t2) //newline { //newline if (t1 == nullptr && t2 != nullptr) return false; //newline if (t1 != nullptr && t2 == nullptr) return false; //newline if (t1 == nullptr && t2 == nullptr) return true; //newline if (t1->v != t2->v) return false; //newline return isM(t1->l, t2->r) && isM(t1->r,t2->l); //newline }//newline }}}