LeetCode 面试经典150题 [67/150 对称二叉树]


avatar
GuoYulong 2024-07-16 98

题目描述

给你一个二叉树的根节点 root , 检查它是否轴对称。
示例 1:

输入:root = [1,2,2,3,4,4,3]
输出:true

示例 2:

输入:root = [1,2,2,null,3,null,3]
输出:false

个人C++解答


递归好难写 不会 哎

GYL
/**
 * 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 root == nullptr || isSy(root->left,root->right);
    }
private:
    bool isSy(TreeNode* l,TreeNode*r){
        if(l == nullptr && r==nullptr) return true;
        if(l==nullptr || r==nullptr || l->val != r->val) return false;
        return isSy(l->left,r->right) && isSy(l->right,r->left);

    }
};

相关阅读

注意!!!

新增会员中心页面,方便管理个人账户,充值功能暂不开启,请勿为本网站进行任何充值活动!!!

通知!!!

① 过年好!!!拖更几个月了已经,年后继续更新!!