(后序遍历 简单)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;

    }
};
相关推荐
1231566807 分钟前
PAT 1017 A除以B
c语言·数据结构·算法·pat考试
努力学算法的蒟蒻14 分钟前
day112(3.14)——leetcode面试经典150
面试·职场和发展
芯片-嵌入式15 分钟前
具身智能(2):OpenExplorer下的模型量化
人工智能·深度学习·算法
Yusei_052317 分钟前
C++14入门
c++·算法
YLXA24 分钟前
1.helle_cuda学习
linux·学习·算法
Storynone27 分钟前
【Day21】LeetCode:93. 复原IP地址,78. 子集,90. 子集 II
python·算法·leetcode
ab15151732 分钟前
3.14二刷基础93 94 83 98 99 完成进阶40 43
算法
nananaij35 分钟前
【LeetCode-01 两数之和 python解法】
开发语言·python·算法·leetcode
crescent_悦35 分钟前
PTA C++:正整数A+B
数据结构·c++·算法
丶小鱼丶37 分钟前
数据结构和算法之【链表】
java·数据结构·算法