当前位置: 首页 > news >正文

力扣100. 相同的树(利用分解思想解决)

Problem: 100. 相同的树

文章目录

  • 题目描述
  • 思路
  • Code

题目描述

在这里插入图片描述在这里插入图片描述在这里插入图片描述

思路

题目要求判断两个二叉树是否完全相同,而此要求可以利用问题分解的思想解决,即判断当前节点的左右子树是否完全相同,而在二叉树问题分解的一般题目中均会带有返回值,具体的此题中当p、q指针均为null时返回true,当其中一个为null时(不是同时为null)返回false,当p、q指针指向的节点值不相同时返回false。

时间复杂度:

O ( n ) O(n) O(n);其中 n n n为二叉树的节点个数

空间复杂度:

O ( h ) O(h) O(h);其中 h h h为二叉树的高度

Code

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null || q == null) {
            return false;
        }
        if (p.val != q.val) {
            return false;
        }
        //Check whether the left and right subtrees are the same
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}

文章转载自:
http://brewster.hfytgp.cn
http://bloody.hfytgp.cn
http://auscultative.hfytgp.cn
http://blackhearted.hfytgp.cn
http://chincapin.hfytgp.cn
http://bodhran.hfytgp.cn
http://acicula.hfytgp.cn
http://amount.hfytgp.cn
http://aequian.hfytgp.cn
http://avarice.hfytgp.cn
http://christingle.hfytgp.cn
http://cassimere.hfytgp.cn
http://cheer.hfytgp.cn
http://bravest.hfytgp.cn
http://acock.hfytgp.cn
http://baluba.hfytgp.cn
http://almsfolk.hfytgp.cn
http://antitrust.hfytgp.cn
http://cabochon.hfytgp.cn
http://aeon.hfytgp.cn
http://blackcurrant.hfytgp.cn
http://calk.hfytgp.cn
http://chitterlings.hfytgp.cn
http://bretton.hfytgp.cn
http://bearward.hfytgp.cn
http://beat.hfytgp.cn
http://bedevil.hfytgp.cn
http://camiknickers.hfytgp.cn
http://aphelion.hfytgp.cn
http://argillaceous.hfytgp.cn
http://www.dtcms.com/a/17643.html

相关文章:

  • 相机与激光雷达联合标定综述
  • 网络安全 | 什么是网络安全?
  • Vue2/Vue3生命周期对比
  • 第十五天 学习并实践HarmonyOS应用的基本结构、页面导航和状态管理
  • C++:构造函数,static成员,友元,内部类
  • 指标+大模型,构建更全、更准、更快的数据分析体验
  • Vue 2 + Webpack 项目中集成 ESLint 和 Prettier
  • 深入浅出理解HBase:大数据时代的“超级仓库”
  • 【大疆无人机地图测绘技术学习:高精度、高效率的全流程解决方案】
  • vue.js之虚拟 DOM
  • 生成对抗网络(GAN)的“对抗“过程解析:从图像合成到药物发现的跨领域应用
  • SpringMVC学习使用
  • 基于Spring Boot的视频点播系统设计与实现(LW+源码+讲解)
  • vue和Django快速创建项目
  • 计算机网络-MPLS基础概念
  • 堡垒机调用xshell 无反应
  • 代码随想录算法【Day45】
  • SpringCloud系列教程:微服务的未来(二十二)RabbitMQ安装部署、快速入门、数据隔离
  • wordpress部署nginx版的
  • Unity使用反射进行Protobuf(CS/SC)协议,json格式
  • 【Audio】Android 10车载音频路由
  • Qt:Qt网络
  • 智能背后的阴影:LLM安全风险
  • C语言中隐式类型转换 截断和整型提升
  • 电脑出现蓝屏英文怎么办?查看修复过程
  • 介绍几款免费的显示器辅助工具!
  • Windows系统中常用的命令
  • 如何使用ADB进行WIFI调试
  • 零售行业线下门店的AI知识库应用实践
  • 可视化实操记录(自用)