(后序遍历 简单)leetcode 101翻转二叉树

将根结点的左右结点看作 两个树的根结点,后序遍历(从叶子结点从下往上遍历)

两个树边遍历边比较。

左节点就左右根的后序遍历

右根结点就右左根的后序遍历来写

后序遍历(从叶子结点从下往上遍历)

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 {
    bool a=true;
public:
    bool isSymmetric(TreeNode* root) {
        bitree(root->left,root->right);
return a;
    }
    
    void bitree(TreeNode *rootl,TreeNode *rootr)
    {
        if(rootl==NULL&&rootr==NULL)
        return ;

        if(rootl==NULL&&rootr!=NULL)
     {   a=false;
            return ;
        }
        if(rootl!=NULL&&rootr==NULL)
     {   a=false;
        return ;

    }

      
        
        bitree(rootl->left,rootr->right);
        bitree(rootl->right,rootr->left);
          if(rootl->val!=rootr->val)
        a=false;

    }
};
相关推荐
天真小巫2 分钟前
2025.4.26总结
职场和发展
Felven11 分钟前
A. Ideal Generator
java·数据结构·算法
懒懒小徐19 分钟前
大厂面试-框架篇
面试·职场和发展
MoonBit月兔40 分钟前
双周报Vol.70: 运算符重载语义变化、String API 改动、IDE Markdown 格式支持优化...多项更新升级!
ide·算法·哈希算法
How_doyou_do44 分钟前
树状数组底层逻辑探讨 / 模版代码-P3374-P3368
数据结构·算法·树状数组
小鹿鹿啊1 小时前
C语言编程--14.电话号码的字母组合
c语言·开发语言·算法
小O的算法实验室1 小时前
2024年ESWA SCI1区TOP:量子计算蜣螂算法QHDBO,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
C语言魔术师2 小时前
509. 斐波那契数
算法·动态规划
共享家95272 小时前
栈相关算法题解题思路与代码实现分享
c++·leetcode
Wendy_robot2 小时前
【前缀和计算和+哈希表查找次数】Leetcode 560. 和为 K 的子数组
c++·算法·leetcode