Check Mirror Tree

Check Mirror Tree

Given two n-ary trees.?Check if they are mirror images of each other or not. You are also given e denoting the number of edges in both trees, and two arrays, A[] and B[]. Each array has?2*e space separated values u,v denoting an edge from u to v for the both trees.


?? View Full Code??

Approach :

  • For each node in Tree 1 (using array A), store its children in an unordered_map where the key is the node, and the value is an array that will store its children in the order they appear in A.
  • Do the same process for Tree 2 using array B.
  • To make Tree 2 a mirror of Tree 1, reverse the map of children in map2 for each node.
  • For each node in the range [1, n], check if map1[node] is equal to map2[node] after reversing map2.
  • If any node’s children don’t match, return 0 (not mirror images). Otherwise, return 1.

int checkMirrorTree(int n, int e, int A[], int B[]) {
        
        unordered_map<int, vector<int>>m1, m2;
        
        for(int i=0 ; i<2*e ; i+=2){
            
            m1[A[i]].push_back(A[i+1]);
            m2[B[i]].push_back(B[i+1]);
        }
        
        for(int i=1 ; i<n+1 ; i++){
            reverse(m1[i].begin(), m1[i].end());
            
            if(m1[i] != m2[i]){
                return 0;
            }
        }
        return 1;   
    }        

Time Complexity : O(e)

Space Complexity : O(e)

?? View GitHub Repo ??



Anmol .

Student || 3??Codechef (Max rating -1629) || 5?? C++/Problem Solving Hackerrank ||Codeforces Max Rating -1166|| CP Enthusiast || Problem Solver & Daily Streak Gold??Codechef || 300+Codechef || 200+ Codeforces

4 个月

Very helpful

要查看或添加评论,请登录

Pranay Saini的更多文章

  • Maximum sum of nodes in Binary tree such that no two are adjacent

    Maximum sum of nodes in Binary tree such that no two are adjacent

    Given a binary tree with a value associated with each node, we need to choose a subset of these nodes such that sum of…

    2 条评论
  • Largest Subtree Sum

    Largest Subtree Sum

    Given a binary tree. The task is to find subtree with maximum sum in the tree and return its sum.

    2 条评论
  • Sum of Longest Blood Line of Tree

    Sum of Longest Blood Line of Tree

    Given a binary tree having n nodes. Find the sum of all nodes on the longest path from root to any leaf node.

  • Duplicate Subtree

    Duplicate Subtree

    Given a binary tree, find out whether it contains a duplicate sub-tree of size two or more, or not. Note: Two same leaf…

    2 条评论
  • Leaves at Same Level

    Leaves at Same Level

    Given a binary tree with n nodes, determine whether all the leaf nodes are at the same level or not. Return true if all…

  • Sum Tree

    Sum Tree

    Given a Binary Tree. Check for the Sum Tree for every node except the leaf node.

  • Transform to Sum Tree

    Transform to Sum Tree

    Given a Binary Tree of size N , where each node can have positive or negative values. Convert this to a tree where each…

    2 条评论
  • Boundary Traversal of Binary Tree

    Boundary Traversal of Binary Tree

    Given a Binary Tree, find its Boundary Traversal. The traversal should be in the following order: Left boundary nodes:…

  • Diagonal Traversal of Binary Tree

    Diagonal Traversal of Binary Tree

    Consider lines of slope -1 passing between nodes. Given a Binary Tree, print all diagonal elements in a binary tree…

    2 条评论
  • Balanced Tree Check

    Balanced Tree Check

    A binary tree is height-balanced if, for every node in the tree, the difference in heights of the left and right…

    2 条评论

社区洞察

其他会员也浏览了