101.对称二叉树

Posted jesseywang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了101.对称二叉树相关的知识,希望对你有一定的参考价值。

题目描述: 给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

DFS:递归,和100题相同的树类似,不过要注意是左右子树进行比较

//C

//注意这个函数声明
bool isMirroTree(struct TreeNode* p, struct TreeNode* q);
bool isSymmetric(struct TreeNode* root){
    return isMirroTree(root, root);
}
bool isMirroTree(struct TreeNode* p, struct TreeNode* q){
    if(p == NULL && q == NULL) return true;
    else if(p == NULL || q == NULL) return false;
    else if(p -> val == q -> val){
        return isMirroTree(p -> left,q -> right) && isMirroTree(p -> right, q -> left);
    }
    return false;
}

//JS
var isSymmetric = function(root) {
    let isMirroTree = function(p, q){
        if(!p && !q) return true;
        if(!p || !q) return false;
        if(p.val != q.val) return false;
        return isMirroTree(p.left, q.right) && isMirroTree(p.right, q.left);
    };
    return isMirroTree(root, root);
};

BFS:迭代,用队列层次遍历,可以使用双队列,也可以单队列

//C

//双队列实现
#define QUEUESIZE 200

bool isSymmetric(struct TreeNode* root){
    if(!root)
        return 1;
    if(!root->left && !root->right)
        return 1;
    if(!root->left || !root->right)
        return 0;

    /*init queue*/
    struct TreeNode* q1[QUEUESIZE];
    struct TreeNode* q2[QUEUESIZE];
    int front1, rear1, front2, rear2;
    front1 = rear1 = front2 = rear2 = -1;
    q1[++rear1] = root->left;
    q2[++rear2] = root->right;

    struct TreeNode *a, *b;
    while(front1 < rear1){
        a = q1[++front1];
        b = q2[++front2];
        if(!a && !b)
            continue;
        if(!a || !b)
            return 0;
        if(a->val != b->val)
            return 0;
        q1[++rear1] = a->left;
        q2[++rear2] = b->right;
        q1[++rear1] = a->right;
        q2[++rear2] = b->left;
    }
    return 1;
}

//单队列实现
#define QUEUESIZE 200

bool isSymmetric(struct TreeNode* root){
    if(!root)
        return 1;
    if(!root->left && !root->right)
        return 1;
    if(!root->left || !root->right)
        return 0;

    /*init queue*/
    struct TreeNode* q[QUEUESIZE];
    int front, rear;
    front = rear = -1;
    q[++rear] = root;
    q[++rear] = root;

    struct TreeNode *a, *b;
    while(front < rear){
        a = q[++front1];
        b = q[++front2];
        if(!a && !b)
            continue;
        if(!a || !b)
            return 0;
        if(a->val != b->val)
            return 0;
        q[++rear] = a->left;
        q[++rear] = b->right;
        q[++rear] = a->right;
        q[++rear] = b->left;
    }
    return 1;
}

//JS

//双队列
var isSymmetric = function(root) {
    if(!root) return true;
    let leftTreeQueue = [], rightTreeQueue = [], leftNode, rightNode;
    leftTreeQueue.push(root.left);
    rightTreeQueue.push(root.right);
    while(leftTreeQueue.length != 0 && rightTreeQueue.length != 0){
        leftNode = leftTreeQueue.shift();
        rightNode = rightTreeQueue.shift();
        if(!leftNode && !rightNode) continue;
        if(!leftNode || !rightNode) return false;
        if(leftNode.val != rightNode.val) return false;
        else{
            leftTreeQueue.push(leftNode.left);
            rightTreeQueue.push(rightNode.right);
            leftTreeQueue.push(leftNode.right);
            rightTreeQueue.push(rightNode.left);
        }
    }
    return true;
};

//单队列
var isSymmetric = function(root) {
    if(!root) return true;
    let treeQueue = [], leftNode, rightNode;
    treeQueue.push(root);
    treeQueue.push(root);
    while(treeQueue.length != 0){
        leftNode = treeQueue.shift();
        rightNode = treeQueue.shift();
        if(!leftNode && !rightNode) continue;
        if(!leftNode || !rightNode) return false;
        if(leftNode.val != rightNode.val) return false;
        else{
            treeQueue.push(leftNode.left);
            treeQueue.push(rightNode.right);
            treeQueue.push(leftNode.right);
            treeQueue.push(rightNode.left);
        }
    }
    return true;
};

  

 

以上是关于101.对称二叉树的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode101----对称二叉树

2021-10-05:对称二叉树。给定一个二叉树,检查它是否是镜像对称的。例如,二叉树 [1,2,2,3,4,4,3] 是对称的。力扣101。

LeetCode101. 对称二叉树

LeetCode第101题—对称二叉树—Python实现

leecode 101. 对称二叉树

代码随想录算法训练营第15天 | ● 层序遍历 10 ● 226.翻转二叉树 ● 101.对称二叉树 2