当前位置:网站首页>Leetcode exercise - Sword finger offer 26 Substructure of tree
Leetcode exercise - Sword finger offer 26 Substructure of tree
2022-07-06 22:26:00 【SK_ Jaco】
1. Title Description
The finger of the sword Offer 26. The substructure of a tree
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
2. Problem solving ideas and codes
2.1 Their thinking
Question request judgment B Is it A The subtree of , So first we need to be in A Find B Root node of , Then start from the root node to judge whether the nodes are the same . So the topic becomes two steps : stay A Search for B Root node of the same node 、 Take this node as the root node and B Compare and judge whether they are the same in turn . Take the topic as an example 2 To illustrate .
First, in the A Find and B Points with the same root node value 
stay A After finding the node in , Take this node as the root node and B Compare from the root node , until B Traverse complete

It should be noted that , The values of nodes in the title may be the same , So you need to A Find each node , And make a comparison every time you find one .
2.2 Code
class Solution {
boolean ans = false;
public boolean isSubStructure(TreeNode A, TreeNode B) {
if (B == null) {
return false;
}
find(A, B);
return ans;
}
// Compare whether the two trees are the same
public boolean compare(TreeNode root, TreeNode compareNode) {
if (root == null) {
return compareNode == null;
}
if (compareNode == null) {
return true;
}
if (root.val != compareNode.val) {
return false;
}
// Compare the leftmost subtree of the current node
boolean p1 = compare(root.left, compareNode.left);
// Compare the right subtree of the current node
boolean p2 = compare(root.right, compareNode.right);
return p1 & p2;
}
public void find(TreeNode root, TreeNode compareNode) {
if (root == null) {
return;
}
if (root.val == compareNode.val) {
// When the value of the current node is the same as that of the tree head node for comparison , Compare
boolean compare = compare(root, compareNode);
ans |= compare;
}
// Find the left subtree
find(root.left, compareNode);
// Find the right subtree
find(root.right, compareNode);
}
}
2.3 test result
Pass the test

3. summary
- According to the requirements of the topic, we need to search and compare the tree
- Every time you find a node with the same value, you need to make a comparison
边栏推荐
- [sciter]: encapsulate the notification bar component based on sciter
- SQL Server生成自增序号
- Shortcut keys in the terminal
- Solve project cross domain problems
- CCNA-思科网络 EIGRP协议
- labelimg的安装与使用
- i. Mx6ull build boa server details and some of the problems encountered
- Xiaoman network model & http1-http2 & browser cache
- Force buckle 575 Divide candy
- [linear algebra] determinant of order 1.3 n
猜你喜欢

The nearest common ancestor of binary (search) tree ●●

3DMax指定面贴图

2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks

二分图判定

Wechat red envelope cover applet source code - background independent version - source code with evaluation points function
![[linear algebra] determinant of order 1.3 n](/img/6e/54f3a994fc4c2c10c1036bee6715e8.gif)
[linear algebra] determinant of order 1.3 n

That's why you can't understand recursion

Aardio - 利用customPlus库+plus构造一个多按钮组件

Powerful domestic API management tool

(十八)LCD1602实验
随机推荐
2021 geometry deep learning master Michael Bronstein long article analysis
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
HDU 2008 digital statistics
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
第3章:类的加载过程(类的生命周期)详解
3DMAX assign face map
anaconda安装第三方包
C#實現水晶報錶綁定數據並實現打印4-條形碼
中国VOCs催化剂行业研究与投资战略报告(2022版)
Wechat red envelope cover applet source code - background independent version - source code with evaluation points function
Installation and use of labelimg
(十八)LCD1602实验
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
Management background --2 Classification list
12、 Start process
QT | UDP broadcast communication, simple use case
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
Attack and defense world miscall