当前位置:网站首页>[LeetCode]572. A subtree of another tree
[LeetCode]572. A subtree of another tree
2022-06-27 21:50:00 【A Fei algorithm】
subject
572. The subtree of another tree
Here are two binary trees root and subRoot . test root Whether and subRoot Subtrees with the same structure and node values . If there is , return true ; otherwise , return false .
Binary tree tree A subtree of includes tree A node of and all descendants of this node .tree It can also be seen as a subtree of its own .
Example 1:
Input :root = [3,4,5,1,2], subRoot = [4,1,2]
Output :true
Example 2:
Input :root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
Output :false
Tips :
root The number of nodes on the tree ranges from [1, 2000]
subRoot The number of nodes on the tree ranges from [1, 1000]
-104 <= root.val <= 104
-104 <= subRoot.val <= 104
Method 1:dfs
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null || subRoot == null) {
return root == null && subRoot == null;
}
// Be careful , If the comparison is root, Compare the same tree :isSameTree
// If the comparison is root Left and right child nodes , Then compare whether it is a subtree :isSubtree
return isSameTree(root, subRoot) ||
isSubtree(root.left, subRoot) ||
isSubtree(root.right, subRoot);
}
private boolean isSameTree(TreeNode source, TreeNode target) {
if (source == null || target == null) {
return source == null && target == null;
}
return source.val == target.val &&
isSameTree(source.left, target.left) &&
isSameTree(source.right, target.right);
}
边栏推荐
- Go from entry to practice - multiple selection and timeout control (notes)
- Go from starting to Real - Interface (note)
- Acwing周赛57-数字操作-(思维+分解质因数)
- Go from introduction to practice - polymorphism (note)
- 流程控制任务
- TypeScript学习
- CEPH distributed storage
- Go from introduction to actual combat - context and task cancellation (notes)
- 我想我要开始写我自己的博客了。
- 01-Golang-环境搭建
猜你喜欢

Go从入门到实战——任务的取消(笔记)

100 important knowledge points that SQL must master: using functions to process data

ICML2022 | 可扩展深度高斯马尔可夫随机场

STM32F107+LAN8720A使用STM32cubeMX配置网络连接+tcp主从机+UDP app

Go from introduction to practice - Interface (notes)

畅游动态规划之区间DP

At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions

vmware虚拟机PE启动

Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer

Knowledge sorting of exception handling
随机推荐
鲜为人知的mysql导入数据
Common methods of string class
io流代码
Go从入门到实战——协程机制(笔记)
Codeforces Round #721 (Div. 2)
100 important knowledge points that SQL must master: filtering data
gomock mockgen : unknown embedded interface
GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析
Go from starting to Real - Interface (note)
qt base64加解密
[LeetCode]161. 相隔为 1 的编辑距离
Go from introduction to actual combat - panic and recover (notes)
SQL必需掌握的100个重要知识点:使用函数处理数据
Tiktok's interest in e-commerce has hit the traffic ceiling?
How to participate in openharmony code contribution
Go从入门到实战——CSP并发机制(笔记)
Quick excel export
Go from entry to practice -- CSP concurrency mechanism (note)
专题教程——选队长游戏
Special tutorial - Captain selection game