当前位置:网站首页>Sword finger offer 26 Substructure of tree
Sword finger offer 26 Substructure of tree
2022-07-07 00:11:00 【xzystart】
Input two binary trees A and B, Judge B Is it right? A Substructure of .( A convention empty tree is not a substructure of any tree )
B yes A Substructure of , namely A There are emergence and B Same structure and node values .
for example :
Given tree A:
3
/ \
4 5
/ \
1 2
Given tree B:
4
/
1
return true, because B And A A subtree of has the same structure and node values .
Example 1:
Input :A = [1,2,3], B = [3,1]
Output :false
Example 2:
Input :A = [3,4,5,1,2], B = [4,1]
Output :true
class Solution {
public boolean isSubStructure(TreeNode A, TreeNode B) {
// Judge by ab A tree for roots b Whether it is a Substructure of
// The function of this function is to judge the two trees of the input parameter ,b Whether it is a Substructure of
// The principle is to see a Whether or not b Exactly the same , If the same returns true, If not, judge a Whether the left and right subtrees of are related to b Exactly the same ,
// The principle is to find A A child node of the tree ( Contains the root node ) Is the subtree of the root and B The trees are exactly equal , If this condition is met , It can be said that it meets the meaning of the topic
// If traversal to empty has not found , It proves that the meaning of the question is not satisfied
if (B == null || A == null) {
// Special value judgment ( Recursion to empty has not been found through the following if)
return false;
}
if (A.val == B.val && (helper(A.left, B.left) && helper(A.right, B.right))) {
// Judge ab Is it completely equal
return true; // There is only one recursive function true return , So the top function is true( Because recursion uses the or operator )
}
// Recursively call ( If the above if If you pass, you will not enter here. Enter here to explain ab A tree with roots does not meet the condition )
// If the above if Pass to prove B Trees and A The molecular structure of a part of the tree is completely equal
return isSubStructure(A.left, B) || isSubStructure(A.right, B);
// Use the or operator , As long as there is one satisfaction, it will be satisfied as a whole
}
private boolean helper(TreeNode root1, TreeNode root2) {
// Judge by ab Whether the trees with roots are completely equal
// Be careful , The function of this function is to judge whether the two trees of the input parameter are completely equal
if (root2 == null) {
return true;
}
if (root1 == null) {
return false;
}
if (root1.val == root2.val) {
return helper(root1.left, root2.left) && helper(root1.right, root2.right);
} else {
return false;
}
}
}
边栏推荐
- iMeta | 华南农大陈程杰/夏瑞等发布TBtools构造Circos图的简单方法
- pytest多进程/多线程执行测试用例
- Competition between public and private chains in data privacy and throughput
- 互动滑轨屏演示能为企业展厅带来什么
- Oracle对表进行的常用修改命令
- Design of short chain
- Data operation platform - data collection [easy to understand]
- vector的使用方法_vector指针如何使用
- GPIO簡介
- TypeScript中使用类型别名
猜你喜欢

Résumé des connaissances de gradle

Interface joint debugging test script optimization v4.0

Racher integrates LDAP to realize unified account login

matplotlib画柱状图并添加数值到图中

What can the interactive slide screen demonstration bring to the enterprise exhibition hall

app通用功能测试用例

Please help xampp to do sqlilab is a black

Three application characteristics of immersive projection in offline display

什么是响应式对象?响应式对象的创建过程?

What is AVL tree?
随机推荐
[CVPR 2022] semi supervised object detection: dense learning based semi supervised object detection
Gradle knowledge generalization
Do you still have to rely on Simba to shout for a new business that is Kwai?
Oracle中使用包FY_Recover_Data.pck来恢复truncate误操作的表
Penetration test --- database security: detailed explanation of SQL injection into database principle
2022 PMP project management examination agile knowledge points (9)
零代码高回报,如何用40套模板,能满足工作中95%的报表需求
Oracle EMCC 13.5 environment in docker every minute
陀螺仪的工作原理
DAY ONE
【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection
自动化测试工具Katalon(Web)测试操作说明
Local deployment Zeppelin 0.10.1
Design a red envelope grabbing system
专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
[212] what are three methods for PHP to send post requests
STM32通过串口进入和唤醒停止模式
How to use vector_ How to use vector pointer
三句话简要介绍子网掩码
openresty ngx_ Lua subrequest