当前位置:网站首页>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;
}
}
}
边栏推荐
- The largest single investment in the history of Dachen was IPO today
- [unmanned aerial vehicle] multi unmanned cooperative task allocation program platform, including Matlab code
- 2022年PMP项目管理考试敏捷知识点(9)
- GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
- [212] what are three methods for PHP to send post requests
- 基于SSM框架实现的房屋租赁管理系统
- 《LaTex》LaTex数学公式简介「建议收藏」
- 【向量检索研究系列】产品介绍
- ldap创建公司组织、人员
- pinia 模块划分
猜你喜欢

Newsletter L Huobi ventures is in-depth contact with genesis public chain

Rider离线使用Nuget包的方法

DAY TWO

互动滑轨屏演示能为企业展厅带来什么

自动化测试工具Katalon(Web)测试操作说明

【自动化测试框架】关于unittest你需要知道的事

零代码高回报,如何用40套模板,能满足工作中95%的报表需求

17、 MySQL - high availability + read / write separation + gtid + semi synchronous master-slave replication cluster

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

GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
随机推荐
[communication] optimal power allocation in the uplink of two-layer wireless femtocell network with matlab code
What is a responsive object? How to create a responsive object?
okcc呼叫中心的订单管理时怎么样的
DAY TWO
The largest single investment in the history of Dachen was IPO today
Every year, 200 billion yuan is invested in the chip field, and "China chip" venture capital is booming
使用yum来安装PostgreSQL13.3数据库
Yaduo Sangu IPO
C语言输入/输出流和文件操作【二】
互动滑轨屏演示能为企业展厅带来什么
Design of short chain
AVL树到底是什么?
Common modification commands of Oracle for tables
Quickly use various versions of PostgreSQL database in docker
快讯 l Huobi Ventures与Genesis公链深入接洽中
web渗透测试是什么_渗透实战
MATLIB reads data from excel table and draws function image
Wasserstein slim gain with gradient poverty (wsgain-gp) introduction and code implementation -- missing data filling based on generated countermeasure network
Why is bat still addicted to 996 when the four-day working system is being tried out in Britain?
在docker中快速使用各个版本的PostgreSQL数据库