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));
    }
};
相关推荐
讳疾忌医丶6 小时前
深度拆解 RocksDB 内核:基于 C++17 的 FIFO 调度状态机与温度阶梯自愈设计
java·c++·算法·架构
lsswear6 小时前
Python 并发 线程
开发语言·python
2401_868534786 小时前
OpenStack 架构全景:八个核心组件深度拆解
c++·需求分析
郑州光合科技余经理6 小时前
国际版外卖系统:税率字段怎么和订单主流程解耦
android·java·开发语言·前端·后端·php·ai编程
软件课代表CiCi6 小时前
运行库是什么?电脑缺运行库怎么办?c++运行库缺失修复全攻略
开发语言·c++·windows·电脑·dll修复·dll丢失·运行库
Ivanqhz7 小时前
MLIR OpBuilder
开发语言·python·mlir
蒸蒸yyyyzwd7 小时前
从面经中学到的三件事
c++·笔记
时时三省8 小时前
【时时三省】计算机c语言二级考试选择题重点
c++
红宝村村长8 小时前
windows笔记本双系统ubuntu设置
开发语言
菜鸟~noob2339 小时前
【电子战】第15篇:混合定位——TDOA/FDOA/AOA 联合【含matlab代码】
开发语言·matlab