A (1170) : A DS二叉树_对称二叉树


一、题目描述

给定二叉树,检查该二叉树是否为镜像对称的。

在这里插入图片描述


二、输入与输出

1.输入

第一行输入t,表示有t个测试样例。
第二行起,每一行首先输入n,接着输入n个整数代表一个二叉树。依次输入t个二叉树。
数组形式的二叉树表示方法与题目:DS二叉树_伪层序遍历构建二叉树 相同,输入-1表示空结点。
结点个数 >= 1
n >= 1
1 <= 结点值 <= 1000

4
7 1 2 2 3 4 4 3
7 1 2 2 -1 3 -1 3
7 10 20 20 -1 40 40 -1
5 1 2 2 -1 3


2.输出

每一行输出二叉树是否为镜像对称。
依次输出t行。

true
false
true
false

三、参考代码

#include <iostream>
#include <string>
#include<queue>
#include<stack>
using namespace std;

class TreeNode
{
public:
    int data;
    TreeNode* lchild;
    TreeNode* rchild;
    TreeNode* parent;
    TreeNode() { lchild = NULL, rchild = NULL; parent = NULL; }
    TreeNode(int n) { lchild = NULL, rchild = NULL; parent = NULL; data = n; }
};




class Tree
{
public:
    TreeNode* root;
    int* arr;
    int len;

    Tree() {};
    ~Tree() {};

    void Create()
    {
        root = Creat();
    }


    
    TreeNode* Creat()
    {
        cin >> len;

        if (len == 0)
        {
            return NULL;
        }

        arr = new int[len];
        for (int i = 0; i < len; i++)
        {
            cin >> arr[i];
        }

        queue<TreeNode*>q;
        TreeNode* r = new TreeNode(arr[0]);

        if (r)
        {
            q.push(r);
        }
        int index = 1;

        while (!q.empty() && index < len)
        {
            TreeNode* cur = q.front();
            q.pop();

            if (index < len && arr[index] != -1)
            {
                cur->lchild = new TreeNode(arr[index]);
                q.push(cur->lchild);
            }
            index++;

            if (index < len && arr[index] != -1)
            {
                cur->rchild = new TreeNode(arr[index]);
                q.push(cur->rchild);
            }
            index++;
        }
        return r;
    }
    

    bool pri()
    {
        if (root == NULL) return false;

        queue<TreeNode*>q;
        q.push(root->lchild);
        q.push(root->rchild);

        while (!q.empty())
        {
            TreeNode* t1 = q.front();
            q.pop();
            TreeNode* t2 = q.front();
            q.pop();

            if ( !t1  && !t2 )//都是NULL 00
            {
                continue;
            }

            if (!t1 || !t2)//一个是NULL一个不是 10/01
            {
                return false;
            }

            //只剩下11
            if (t1->data != t2->data)
            {
                return false;
            }

            q.push(t1->lchild);
            q.push(t2->rchild);
            q.push(t1->rchild);
            q.push(t2->lchild);
        }
        return true;
        
    }


};

int main()
{
    int sum;
    cin >> sum;

    while (sum--)
    {
        Tree t;
        t.Create();
        if (t.pri())
        {
            cout << "true" << endl;
        }
        else
        {
            cout << "false" << endl;
        }

    }
    return 0;
}




  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z1Jxxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值