记录刷题——(leetcode——101对称二叉树)

题目:给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/
2 2
/ \ /
3 4 4 3
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/symmetric-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool is_symmetric(struct TreeNode* root1,struct TreeNode* root2){
    
    
    if(!root1&&!root2)
        return true;
    if(!root1||!root2){
    
    
        return false;
    }
    if(root1->val==root2->val){
    
    
        return is_symmetric(root1->left,root2->right)&&is_symmetric(root1->right,root2->left);
    }
    return false;
}
bool isSymmetric(struct TreeNode* root){
    
    
    return is_symmetric(root,root);
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lthahaha/article/details/106463200
今日推荐