力扣刷题之对称二叉树(二)

简介: 力扣刷题之对称二叉树(二)

前言


本节是根据上篇博客遗留下的问题进行讲解,大家可以看下这篇博客:

力扣刷题之对称二叉树(一)_skeet follower的博客-CSDN博客

相同的树


6e053908cf62433aaf45d01db9faac09.png这题就是前一章题目的变形,方法基本一致,这里不做过多介绍,大家可以看下上篇博客

递归


class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
      if(p==nullptr&&q==nullptr)
      {
          return true;
      }  
      if(p==nullptr||q==nullptr)
      {
          return false;
      }
      if(p->val!=q->val)
      {
          return false;
      }
     return  isSameTree(p->left,q->left)&& isSameTree(p->right,q->right);
    }
};

迭代


class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        queue<TreeNode*> que;
        que.push(p);
        que.push(q);
        while (!que.empty()) {
            TreeNode* tree1 = que.front(); que.pop();
            TreeNode* tree2 = que.front(); que.pop();
            if (!tree1 && !tree2) continue;
            if (!tree1 || !tree2 || tree1->val != tree2->val) return false;
            que.push(tree1->left);
            que.push(tree2->left);
            que.push(tree1->right);
            que.push(tree2->right);
        }
        return true;
    }
};

另一颗树的子树


20b2979d75524f8781725e935aada2a0.png

递归


class Solution {
public:
    bool compare(TreeNode* root, TreeNode* subRoot) {
        //如果两个都为空
        if(root==NULL&&subRoot==NULL){
            return true;
        }
        //如果只有一个为空
        if(root==NULL||subRoot==NULL){
            return false;
        }
        //如果两个都不空,结点值也不同,那直接返回false
        if(root->val!=subRoot->val){
            return false;
        }
        //如果现在结点值和子树结点值相同,再分别检查两个的左右孩子
        return compare(root->left, subRoot->left) && compare(root->right, subRoot->right);
    }
    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        //如果要检查的子树为空,那么不用查了,肯定对的
        if(subRoot == NULL) return true;
        //如果要检查的子树不空,但root是空的,那也不用查了,错的。
        if(root == NULL) return false;
        //要么是它本身,要么是它左子树,要么是它的右子树
        return compare(root, subRoot) || isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
    }
};
相关文章
|
28天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
28天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
12天前
【LeetCode 31】104.二叉树的最大深度
【LeetCode 31】104.二叉树的最大深度
12 2
|
12天前
【LeetCode 29】226.反转二叉树
【LeetCode 29】226.反转二叉树
12 2
|
12天前
【LeetCode 28】102.二叉树的层序遍历
【LeetCode 28】102.二叉树的层序遍历
10 2
|
12天前
【LeetCode 43】236.二叉树的最近公共祖先
【LeetCode 43】236.二叉树的最近公共祖先
10 0
|
12天前
【LeetCode 38】617.合并二叉树
【LeetCode 38】617.合并二叉树
8 0
|
12天前
【LeetCode 37】106.从中序与后序遍历构造二叉树
【LeetCode 37】106.从中序与后序遍历构造二叉树
9 0
|
12天前
【LeetCode 34】257.二叉树的所有路径
【LeetCode 34】257.二叉树的所有路径
8 0
|
12天前
【LeetCode 32】111.二叉树的最小深度
【LeetCode 32】111.二叉树的最小深度
10 0