composer 检查镜像_检查N元树中的镜像

composer 检查镜像

Problem statement:

问题陈述:

Given two n-ary trees, the task is to check if they are mirrors of each other or not.

给定两个n元树,任务是检查它们是否互为镜像。

Note: you may assume that root of both the given tree as 1.

注意:您可以假设两个给定树的根均为1。

Input:

输入:

The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. The first line of each test case contains two space-separated values N and M denoting the no. of nodes and edges respectively. Then in the next line, two lines are 2*M space-separated values u, v denoting an edge from u to v for both trees.

输入的第一行包含一个整数T,表示测试用例的数量。 然后是T测试用例。 每个测试用例的第一行包含两个以空格分隔的值NM,表示编号。 分别为节点和边。 然后在下一行中,两行是2 * M个空格分隔的值uv表示两棵树从uv的边。

Output:

输出:

For each test case in a new line print "YES" if both the trees are mirrors of each other else print "NO".

对于新行中的每个测试用例,如果两棵树都是彼此的镜像,则打印“是”,否则打印“否”。

Examples:

例子:

INPUT:	
T=1
N=3,M=3
1 2 1 3 2 4
1 3 1 2 2 4

OUTPUT: 
YES
    1                  1
   / \                / \
  2   3              3   2
 /                        \
 4                         4

Since they are mirror of each other.

INPUT:
T=1
N=4,M=4
1 2 1 3 2 4 2 5
1 3 1 2 2 4 2 5

OUTPUT: 
NO
    1                     1
   / \                   / \
  2   3                 3   2
 / \                       / \
4   5                     4   5

Here, 
node 4 and 5 are not as in mirror so the tree is not mirror.

Solution Approach

解决方法

We will use stack and queue data structure since stack follow LIFO that is last in first out the way and queue follow FIFO first in first out pattern, is the trees are mirror then the top of the stack will be equal to the front of the queue and if they aren't equal it means that they are not the mirror of each other.

我们将使用堆栈和队列数据结构,因为堆栈遵循后进先出的方式,而队列遵循先进先出的先入先出模式,因为树是镜像的,因此堆栈的顶部等于队列的前端如果它们不相等,则意味着它们不是彼此的镜像。

We will follow this approach, taking each node at a time and checking its connected component in stack and queue. For checking whether each subtree in itself is a mirror or not we will use a boolean variable flag, initially, the flag is true and each time we check if the top of stack and queue front are equal or not, if not then simply return NO as the answer and after checking all nodes return true if all are valid nodes.

我们将采用这种方法,一次获取每个节点,并在堆栈和队列中检查其连接的组件。 为了检查每个子树本身是否是镜像,我们将使用布尔变量标志,该标志最初为true,并且每次我们检查栈顶和队列前部是否相等时(如果不相等),则简单地返回NO作为答案,并且在检查所有节点后,如果所有节点都是有效节点,则返回true。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
    ll t;

    cout << "Enter number of test cases: ";
    cin >> t;

    while (t--) {
        ll n, m;

        cout << "Enter number of nodes and edges: ";
        cin >> n >> m;

        // declare a vector of stacks of size (n+1)
        // since index start from 0.
        stack<int> v1[n + 1];
        // declare a vector of queue of size (n+1)
        // since index start from 0.
        queue<int> v2[n + 1];

        cout << "Enter tree 1: ";
        for (ll i = 0; i < m; i++) {
            ll x, y; //enter the elements of its.
            cin >> x >> y;
            v1[x].push(y);
        }

        cout << "Enter tree 2: ";
        for (ll i = 0; i < m; i++) {
            ll x, y;
            cin >> x >> y; //enter elements of tree 2.
            v2[x].push(y);
        }
        bool flag;
        // iterate through each node.
        for (int i = 1; i <= n; i++) {
            // store nodes of tree 1 connected components in stack
            stack<int>& s = v1[i];
            // store nodes of tree 1 connected components in queue
            queue<int>& q = v2[i];

            // declare a boolean variable to check mirror
            // property among the elements.
            flag = true;
            //compare the stack top to queue top.
            while (!s.empty() and !q.empty()) {
                // if not similar then break from the loop.
                if (s.top() != q.front()) {
                    flag = false;
                    break;
                }
                s.pop();
                q.pop();
            }
            // if not similar then break from the nodes loop
            // since no further comparison is needed.
            if (flag == false)
                break;
        }

        // check if mirror or not.
        cout << "Is Mirror: ";
        if (flag == true)
            cout << "YES"
                 << "\n";
        else
            cout << "NO"
                 << "\n";
    }

    return 0;
}

Output:

输出:

Enter number of test cases: 3
Enter number of nodes and edges: 4 4 
Enter tree 1: 1 2 1 3 2 4 2 5
Enter tree 2: 1 2 1 3 2 5 2 4
Is Mirror: NO
Enter number of nodes and edges: 3 2
Enter tree 1: 1 2 1 3
Enter tree 2: 1 3 1 2
Is Mirror: YES
Enter number of nodes and edges: 3 3
Enter tree 1: 1 2 1 3 2 4
Enter tree 2: 1 3 1 2 2 4
Is Mirror: YES

  • The time complexity for the above approach in worst case: O(n*n)

    最坏情况下上述方法的时间复杂度: O(n * n)

  • Space complexity for the above approach in worst case : O(n)

    上述方法在最坏情况下的空间复杂度: O(n)



Also tagged in: Amazon, DE-Shaw, Hike, MakeMyTrip

也标记在: 亚马逊DE-Shaw远足MakeMyTrip

Problem source: https://practice.geeksforgeeks.org/problems/check-mirror-in-n-ary-tree/0

问题来源:https://practice.geeksforgeeks.org/problems/check-mirror-in-n-ary-tree/0

翻译自: https://www.includehelp.com/icp/check-mirror-in-n-ary-tree.aspx

composer 检查镜像

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值