当前位置:网站首页>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
边栏推荐
- 2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
- Assembly and interface technology experiment 5-8259 interrupt experiment
- Unity3d Learning Notes 6 - GPU instantiation (1)
- 手写ABA遇到的坑
- CCNA Cisco network EIGRP protocol
- 自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
- Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
- go多样化定时任务通用实现与封装
- Support multiple API versions in flask
- 二叉(搜索)树的最近公共祖先 ●●
猜你喜欢
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
Installation and use of labelimg
Management background --3, modify classification
NetXpert XG2帮您解决“布线安装与维护”难题
Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
The nearest common ancestor of binary (search) tree ●●
The SQL response is slow. What are your troubleshooting ideas?
0 basic learning C language - digital tube
Classic sql50 questions
MySQL数据库基本操作-DML
随机推荐
C#實現水晶報錶綁定數據並實現打印4-條形碼
Learn the principle of database kernel from Oracle log parsing
Management background --2 Classification list
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
Common sense: what is "preservation" in insurance?
Aardio - 通过变量名将变量值整合到一串文本中
Wechat red envelope cover applet source code - background independent version - source code with evaluation points function
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
二分图判定
[sdx62] wcn685x will bdwlan Bin and bdwlan Txt mutual conversion operation method
12、 Start process
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
HDR image reconstruction from a single exposure using deep CNN reading notes
Support multiple API versions in flask
数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
lora同步字设置
LeetCode 练习——剑指 Offer 26. 树的子结构
CCNA Cisco network EIGRP protocol
二叉(搜索)树的最近公共祖先 ●●