当前位置:网站首页>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;
}
}
}
边栏推荐
- What can the interactive slide screen demonstration bring to the enterprise exhibition hall
- Use type aliases in typescript
- pytest多进程/多线程执行测试用例
- MySQL主从之多源复制(3主1从)搭建及同步测试
- PostgreSQL uses pgpool II to realize read-write separation + load balancing
- Oracle对表进行的常用修改命令
- 华为mate8电池价格_华为mate8换电池后充电巨慢
- Things like random
- App general function test cases
- MVC and MVVM
猜你喜欢

DAY TWO

Rider离线使用Nuget包的方法

Racher integrates LDAP to realize unified account login

DAY ONE

沉浸式投影在线下展示中的三大应用特点

Basic chart interpretation of "Oriental selection" hot out of circle data
![[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test](/img/8d/b59cf466031f36eb50d4d06aa5fbe4.jpg)
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test

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

DAY ONE

Automatic test tool katalon (WEB) test operation instructions
随机推荐
(LeetCode)两数之和
在Docker中分分钟拥有Oracle EMCC 13.5环境
刘永鑫报告|微生物组数据分析与科学传播(晚7点半)
[system analyst's road] Chapter 7 double disk system design (service-oriented development method)
MySQL master-slave multi-source replication (3 master and 1 slave) setup and synchronization test
PostgreSQL highly available repmgr (1 master 2 slave +1witness) + pgpool II realizes master-slave switching + read-write separation
Racher integrates LDAP to realize unified account login
Rider离线使用Nuget包的方法
STM32 enters and wakes up the stop mode through the serial port
What is a responsive object? How to create a responsive object?
There are only two TXT cells in the ArrayExpress database. Can you only download the sequencing run matrix from line to ENA?
【212】php发送post请求有哪三种方法
17、 MySQL - high availability + read / write separation + gtid + semi synchronous master-slave replication cluster
Who said that new consumer brands collapsed? Someone behind me won
Tourism Management System Based on jsp+servlet+mysql framework [source code + database + report]
Wasserstein slim gain with gradient poverty (wsgain-gp) introduction and code implementation -- missing data filling based on generated countermeasure network
[boutique] Pinia Persistence Based on the plug-in Pinia plugin persist
How rider uses nuget package offline
GPIO簡介
Basic chart interpretation of "Oriental selection" hot out of circle data