当前位置:网站首页>leetcode-另一棵树的子树
leetcode-另一棵树的子树
2022-08-05 02:00: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);
}
}
边栏推荐
- AI+小核酸药物|Eleven完成2200万美元种子轮融资
- 硬实力和软实力,哪个对测试人来说更重要?
- Greenplum Database Fault Analysis - Can a Soft Connection Be Made to the Database Base Folder?
- 英特尔 XDC 2022 精彩回顾:共建开放生态,释放“基建”潜能
- 意识形态的机制
- "Dilili, wait for the lights, wait for the lights", the prompt sound for safe production in the factory
- Chapter 09 Use of Performance Analysis Tools [2. Index and Tuning] [MySQL Advanced]
- Use of pytorch: Convolutional Neural Network Module
- iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
- STM32使用stm32cubemx LL库系列教程
猜你喜欢
随机推荐
【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
迅睿cms网站搬迁换了服务器后网站不能正常显示
HOG feature study notes
LPQ(局部相位量化)学习笔记
tcp中的三次握手与四次挥手
Exploding the circle of friends, Alibaba produced billion-level concurrent design quick notes are too fragrant
Log an error encountered when compiling google gn "I could not find a ".gn" file ..."
行业现状?互联网公司为什么宁愿花20k招人,也不愿涨薪留住老员工~
Short domain name bypass and xss related knowledge
第十四天&postman
[GYCTF2020]EasyThinking
高数_复习_第1章:函数、极限、连续
the mechanism of ideology
使用OpenVINO实现飞桨版PGNet推理程序
ExcelPatternTool: Excel表格-数据库互导工具
AI+小核酸药物|Eleven完成2200万美元种子轮融资
day14--postman接口测试
Transfer Learning - Distant Domain Transfer Learning
直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?