当前位置:网站首页>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);
}
}
边栏推荐
- QStyle平台风格
- Ant Sword Advanced Module Development
- Access Characteristics of Constructor under Inheritance Relationship
- SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型
- Compressed storage of special matrices
- [Fortune-telling-60]: "The Soldier, the Tricky Way"-2-Interpretation of Sun Tzu's Art of War
- 散列表的查找(哈希表)
- LeetCode uses the minimum cost to climb the stairs----dp problem
- 【C语言】详解栈和队列(定义、销毁、数据的操作)
- C student management system head to add a student node
猜你喜欢
随机推荐
树表的查找
post-study program
程序员的七夕浪漫时刻
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
mysql没法Execute 大拿们求解
转:查尔斯·汉迪:你是谁,比你做什么更重要
22-07-31周总结
[ROS](10)ROS通信 —— 服务(Service)通信
LeetCode使用最小花费爬楼梯----dp问题
Using OpenVINO to implement the flying paddle version of the PGNet inference program
mysql树状结构查询问题
注意潍坊开具发票一般需要注意
QT语言文件制作
Compressed storage of special matrices
Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"
js中try...catch和finally的用法
C学生管理系统 头添加学生节点
Flink 1.15.1 集群搭建(StandaloneSession)
[C language] Detailed explanation of stacks and queues (define, destroy, and data operations)
1527. 患某种疾病的患者






![[Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?](/img/81/2dcb61fd6c30f726804c73cf2b3384.jpg)


