当前位置:网站首页>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
边栏推荐
- Aardio - 不声明直接传float数值的方法
- 2022-07-05 使用tpcc对stonedb进行子查询测试
- Build op-tee development environment based on qemuv8
- Chapter 4: talk about class loader again
- 墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航
- GNN, please deepen your network layer~
- Leetcode question brushing (XI) -- sequential questions brushing 51 to 55
- void关键字
- That's why you can't understand recursion
- What are the interface tests? What are the general test points?
猜你喜欢
软考高级(信息系统项目管理师)高频考点:项目质量管理
[线性代数] 1.3 n阶行列式
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
0 basic learning C language - interrupt
0 basic learning C language - digital tube
基於 QEMUv8 搭建 OP-TEE 開發環境
Barcodex (ActiveX print control) v5.3.0.80 free version
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
2022年6月国产数据库大事记-墨天轮
随机推荐
Aardio - 不声明直接传float数值的方法
Management background --1 Create classification
第3章:类的加载过程(类的生命周期)详解
[sciter]: encapsulate the notification bar component based on sciter
小常识:保险中的“保全”是什么?
PVL EDI project case
数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
Powerful domestic API management tool
中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
Chapter 3: detailed explanation of class loading process (class life cycle)
Senior soft test (Information System Project Manager) high frequency test site: project quality management
Xiaoman network model & http1-http2 & browser cache
Common sense: what is "preservation" in insurance?
小程序系统更新提示,并强制小程序重启并使用新版本
如何用程序确认当前系统的存储模式?
Management background --2 Classification list
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
新手程序员该不该背代码?
Installation and use of labelimg
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode