C++速通LeetCode简单第11题-对称二叉树

递归法:

cpp 复制代码
/**
 * 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 isMirror(root,root);    
    }

    bool isMirror(TreeNode* t1,TreeNode* t2)
    {
        if(t1==nullptr&&t2==nullptr)
        return true;

        if(t1==nullptr||t2==nullptr)
        return false;

        return((t1->val==t2->val)&&isMirror(t1->right,t2->left)&&isMirror(t1->left,t2->right));
    }
};
相关推荐
saltymilk8 小时前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥8 小时前
C++ lambda 匿名函数
c++
沐怡旸14 小时前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥15 小时前
C++ 内存管理
c++
博笙困了21 小时前
AcWing学习——双指针算法
c++·算法
感哥21 小时前
C++ 指针和引用
c++
感哥1 天前
C++ 多态
c++
沐怡旸2 天前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4162 天前
Javer 学 c++(十三):引用篇
c++·后端
感哥2 天前
C++ std::set
c++