当前位置:网站首页>leetcode - a subtree of another tree
leetcode - a subtree of another tree
2022-08-05 02:38:00 【qq_52025208】
题目描述:
给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树.
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if(root == null || subRoot == null) return false;
if(isSameTree(root,subRoot)) return true;
if(isSubtree(root.left,subRoot)) return true;
if(isSubtree(root.right,subRoot)) return true;
return false;
}
public static boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q != null || p != null && q == null) {
return false;
}
if(p == null && q == null) {
return true;
}
if(p.val != q.val) {
return false;
}
return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
}
边栏推荐
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
- [C language] Detailed explanation of stacks and queues (define, destroy, and data operations)
- Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
- select 标签自定义样式
- js中try...catch和finally的用法
- leetcode 15
- Quickly learn chess from zero to one
- 01 [Foreword Basic Use Core Concepts]
- 使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
- 软链接引发的物理备份问题
猜你喜欢

lua学习
![01 [Foreword Basic Use Core Concepts]](/img/90/67537d5fad28d68766ca85b887839e.png)
01 [Foreword Basic Use Core Concepts]

2022-08-04: Input: deduplicated array arr, the numbers in it only contain 0~9.limit, a number.Return: The maximum number that can be spelled out with arr if the requirement is smaller than limit.from

DAY23: Command Execution & Code Execution Vulnerability

使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据

使用SuperMap iDesktopX数据迁移工具迁移地图文档和符号

nodeJs--封装路由

【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?

Access Characteristics of Constructor under Inheritance Relationship

学习笔记-----左偏树
随机推荐
Opening - Open a new .NET modern application development experience
Access Characteristics of Constructor under Inheritance Relationship
从零到一快速学会三子棋
QT MV\MVC结构
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
力扣-二叉树的最大的深度
使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
[C language] Detailed explanation of stacks and queues (define, destroy, and data operations)
特殊矩阵的压缩存储
nodeJs--封装路由
数据增强Mixup原理与代码解读
软链接引发的物理备份问题
协作D2D局部模型聚合的半分散联合学习
Note that Weifang generally needs to pay attention to issuing invoices
Gantt chart is here, project management artifact, template is used directly
SDC简介
[Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?
Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
Review 51 MCU