【力扣 - 对称二叉树】

题目描述

给你一个二叉树的根节点 root , 检查它是否轴对称。
在这里插入图片描述
在这里插入图片描述

提示:

树中节点数目在范围 [1, 1000]

-100 <= Node.val <= 100

题解1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
// Helper function to check if two trees are symmetric
bool check(struct TreeNode* p, struct TreeNode* q)
{
    // If both nodes are NULL, they are symmetric
    if(p == NULL && q == NULL)
        return true;
    
    // If one node is NULL and the other is not, they are not symmetric
    if(p == NULL || q == NULL)
        return false;
    
    // If the values of the nodes are equal, recursively check their children
    if(p->val == q->val)
        return check(p->left, q->right) && check(p->right, q->left);
    else
        return false;
}

// Main function to check if a binary tree is symmetric
bool isSymmetric(struct TreeNode* root){
    // Call the helper function with the root node to check symmetry
    return check(root, root);
}

题解2

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool isSymmetric(struct TreeNode* root){
    if (root == NULL) return true; // If the root is NULL, it is symmetric
    return fun(root->left, root->right); // Check symmetry of left and right subtrees
}

int fun(struct TreeNode* l_root, struct TreeNode* r_root){
    if (l_root == NULL && r_root == NULL) return true; // If both nodes are NULL, they are symmetric
    if (l_root == NULL || r_root == NULL) return false; // If one node is NULL and the other is not, they are not symmetric

    return (l_root->val == r_root->val) && // Check if values of nodes are equal
           fun(l_root->left, r_root->right) && // Recursively check left of left subtree with right of right subtree
           fun(l_root->right, r_root->left); // Recursively check right of left subtree with left of right subtree
}
### 力扣(LeetCode)对称二叉树问题的 C++ 实现 以下是基于引用内容以及常见算法设计的一种解决方法,用于判断给定的二叉树是否是对称的。 #### 方法概述 可以通过层次遍历来验证一棵二叉树是否对称。具体来说,可以利用单个队列存储节点并按照特定顺序访问它们。每次从队列中取出两个节点进行比较,确保这两个节点满足镜像关系的要求[^3]。 #### 代码实现 以下是一个完整的 C++ 实现: ```cpp #include <queue> using namespace std; // 定义二叉树结构体 struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; bool isSymmetric(TreeNode* root) { if (!root) return true; // 如果根为空,则认为是对称的 queue<TreeNode*> q; q.push(root->left); q.push(root->right); while (!q.empty()) { TreeNode* L = q.front(); q.pop(); TreeNode* R = q.front(); q.pop(); if (!L && !R) continue; // 左右都为空,继续下一轮 if (!L || !R) return false; // 只有一个为空,不对称 if (L->val != R->val) return false; // 值不相等,不对称 // 按照镜像顺序加入队列 q.push(L->left); q.push(R->right); q.push(L->right); q.push(R->left); } return true; // 所有节点均通过检验 } ``` #### 关键点解析 1. **边界条件处理**:当输入的 `root` 是空时,返回 `true` 表示该情况下的二叉树对称的。 2. **队列操作逻辑**:使用一个队列按顺序交替存储左子树和右子树中的对应节点,并逐一比较这些节点是否具有相同的值。 3. **终止条件**:一旦发现任何一对节点不符合镜像关系,立即返回 `false`;如果整个过程顺利完成而未发现问题,则最终返回 `true`。 此解法的时间复杂度为 O(n),其中 n 是二叉树中的节点总数,因为每个节点仅被访问一次。空间复杂度取决于使用的队列大小,在最坏情况下可能达到 O(w),w 是树的最大宽度。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

六月悉茗

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

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

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

打赏作者

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

抵扣说明:

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

余额充值