記錄刷題——(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);
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章