101. 对称二叉树(C++题解含VS可运行源程序)

这篇博客介绍了如何判断一棵二叉树是否是对称的,提供了两种解法:递归和迭代。递归方法通过比较根节点及其对应节点的值和子树的镜像关系来实现;迭代方法则使用队列,按相反顺序存储节点的左右子节点。代码示例使用C++编写,并可在Visual Studio环境下运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.题解

1.1递归

  • 该问题可以转化为:两个树在什么情况下互为镜像?
  • 如果同时满足下面的条件,两个树互为镜像:
  • 1.它们的两个根结点具有相同的值。
  • 2.每个树的右子树都与另一个树的左子树镜像对称。

1.2迭代(队列)

  • 使用两个队列,将两个结点的左右子结点按相反的顺序插入队列中

2.力扣C++源码

1.1递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        return isMirror(root, root);
    }
    bool isMirror(TreeNode* p1, TreeNode* p2) {
        if (!p1 && !p2)
        {
            return true;
        }
        if (!p1 || !p2)
        {
            return false;
        }
        if (p1->val == p2->val)
            return isMirror(p1->left, p2->right) && isMirror(p1->right, p2->left);
        return false;
    }
};

1.2迭代(队列)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if (!root) return true;
        queue<TreeNode*> p1, p2;
        p1.push(root->left);
        p2.push(root->right);

        while (!p1.empty() && !p2.empty()) {
            TreeNode* node1 = p1.front();
            TreeNode* node2 = p2.front();
            p1.pop();
            p2.pop();
            if ((node1 && !node2) || (!node1 && node2)) return false;
            if (node1) {
                if (node1->val != node2->val) return false;
                p1.push(node1->left);
                p1.push(node1->right);
                p2.push(node2->right);
                p2.push(node2->left);
            }
        }
        return true;
    }
};

3.VS可运行源程序

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<limits>
#include<algorithm>
#include<math.h>
#pragma warning(disable:4996)
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
//一.递归
//该问题可以转化为:两个树在什么情况下互为镜像?
//如果同时满足下面的条件,两个树互为镜像:
//1.它们的两个根结点具有相同的值。
//2.每个树的右子树都与另一个树的左子树镜像对称。
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        return isMirror(root, root);
    }
    bool isMirror(TreeNode* p1, TreeNode* p2) {
        if (!p1 && !p2)
        {
            return true;
        }
        if (!p1 || !p2)
        {
            return false;
        }
        if (p1->val == p2->val)
            return isMirror(p1->left, p2->right) && isMirror(p1->right, p2->left);
        return false;
    }
};
//二.迭代
//两个队列,将两个结点的左右子结点按相反的顺序插入队列中
//class Solution {
//public:
//    bool isSymmetric(TreeNode* root) {
//        if (!root) return true;
//        queue<TreeNode*> p1, p2;
//        p1.push(root->left);
//        p2.push(root->right);
//
//        while (!p1.empty() && !p2.empty()) {
//            TreeNode* node1 = p1.front();
//            TreeNode* node2 = p2.front();
//            p1.pop();
//            p2.pop();
//            if ((node1 && !node2) || (!node1 && node2)) return false;
//            if (node1) {
//                if (node1->val != node2->val) return false;
//                p1.push(node1->left);
//                p1.push(node1->right);
//                p2.push(node2->right);
//                p2.push(node2->left);
//            }
//        }
//        return true;
//    }
//};

//递归方式创建与遍历
void CreateBiTree3(TreeNode** T)//传的是根指针的指针,这样不用返回值了就
{
    int val;
    scanf("%d", &val);
    if (val == -1)//将叶子节点都设为-1,作为判断输入终止标志,#这个根节点就是NULL
        *T = NULL;//*T就是BiTree T的T或者是BiTNode* T的T
    else
    {
        *T = (TreeNode*)malloc(sizeof(TreeNode));
        if (!*T)//开辟空间失败就退出
            exit(-1);
        (*T)->val = val;
        CreateBiTree3(&(*T)->left);//递归创建
        CreateBiTree3(&(*T)->right);
    }
}
int main()
{
    printf("按先序遍历顺序输入二叉树(叶子节点的左右孩子节点均输入-1):\n");
    TreeNode* T;
    CreateBiTree3(&T);//地址传递,传根节点指针的地址
    Solution test;
    bool ans = test.isSymmetric(T);
    printf("%d", ans);

	printf("\n");
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

卜凡.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值