当前位置:网站首页>Sword finger offer 26. substructure of tree
Sword finger offer 26. substructure of tree
2022-07-29 08:37:00 【ThE wAlkIng D】
Title Description
Problem analysis
This problem uses recursive method to find :
First, judge from the root node B Is it right? A Substructure of , If not judging from the left and right subtrees respectively , Just one for true, Just explain B yes A Substructure of
If B It's empty ,A If it is not empty, the traversal ends
If B Not empty ,A It's empty , Or if the values of these two nodes are different, return false. Then compare A,B The corresponding left and right subtrees of nodes .
Code instance
class Solution {
public boolean isSubStructure(TreeNode A, TreeNode B) {
if(A == null && B == null){
return true;
}
if(A == null || B == null){
return false;
}
if(ispart(A,B)){
return true;
}
return isSubStructure(A.left,B) || isSubStructure(A.right,B);
}
private boolean ispart(TreeNode A, TreeNode B){
if(B == null){
return true;
}
if(A == null || A.val != B.val){
return false;
}
return ispart(A.left,B.left) && ispart(A.right,B.right);
}
}
边栏推荐
- C language sorts n integers with pointers pointing to pointers
- Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT
- 数仓分层设计及数据同步问题,,220728,,,,
- (视频+图文)机器学习入门系列-第3章 逻辑回归
- Day15: the file contains the vulnerability range manual (self use file include range)
- Thrift installation manual
- 正则表达式校验版本号
- A little knowledge [synchronized]
- PostgreSQL手动创建HikariDataSource解决报错Cannot commit when autoCommit is enabled
- Proteus simulation based on msp430f2491
猜你喜欢
Back up Google or other browser plug-ins
Application of explosion-proof inclination sensor in safe operation of LNG
Clickhouse learning (III) table engine
数学建模——微分方程
Simulation of four way responder based on 51 single chip microcomputer
数仓分层设计及数据同步问题,,220728,,,,
PostgreSQL manually creates hikaridatasource to solve the error cannot commit when autocommit is enabled
Gan: generate adversarial networks
The computer video pauses and resumes, and the sound suddenly becomes louder
ADB common command list
随机推荐
Requests library simple method usage notes
Day6: use PHP to write file upload page
Basic crawler actual combat case: obtaining game product data
Eggjs create application knowledge points
What are the backup and recovery methods of gbase 8s database
Reading of false news detection papers (3): semi supervised content-based detection of misinformation via tensor embeddings
Normal visualization
Analysis of zorder sampling partition process in Hudi - "deepnova developer community"
Week 2: convolutional neural network basics
Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT
【Transformer】ATS: Adaptive Token Sampling For Efficient Vision Transformers
Pnpm install appears: err_ PNPM_ PEER_ DEP_ ISSUES Unmet peer dependencies
QT learning: use non TS files such as json/xml to realize multilingual internationalization
Data warehouse layered design and data synchronization,, 220728,,,,
Intelligent temperature control system
WQS binary learning notes
2022 Teddy cup data mining challenge C project and post game summary
DC motor speed regulation system based on 51 single chip microcomputer (use of L298)
Inclination sensor is used for long-term monitoring of communication tower and high-voltage tower
分段分页以及段页结合