当前位置:网站首页>LeetCode 练习——剑指 Offer 26. 树的子结构
LeetCode 练习——剑指 Offer 26. 树的子结构
2022-07-06 14:31:00 【SK_Jaco】
1.题目描述
剑指 Offer 26. 树的子结构
输入两棵二叉树A和B,判断B是不是A的子结构。(约定空树不是任意一个树的子结构)
B是A的子结构, 即 A中有出现和B相同的结构和节点值。
例如:
给定的树 A:
3
/ \
4 5
/ \
1 2
给定的树 B:
4
/
1
返回 true,因为 B 与 A 的一个子树拥有相同的结构和节点值。
示例 1:
输入:A = [1,2,3], B = [3,1]
输出:false
示例 2:
输入:A = [3,4,5,1,2], B = [4,1]
输出:true
2.解题思路与代码
2.1 解题思路
题目要求判断 B 是否为 A 的子树,那么首先我们就需要在 A 中找到 B 的根结点,然后从根节点开始以此判断节点是否相同。因此题目就变成了两个步骤:在 A 中寻找 B 的根结点相同的节点、以该节点为根结点和 B 依次比较判断是否相同。以题目示例 2 进行图解。
首先在 A 中寻找和 B 根结点值相同的点
在 A 中找到节点以后,以该节点为根结点与 B 从根节点开始进行比较,直到 B 遍历完成

需要注意的是,题目中节点的值可能相同,因此需要对 A 每个节点进行查找,并且每查找到一个就进行一次比较。
2.2 代码
class Solution {
boolean ans = false;
public boolean isSubStructure(TreeNode A, TreeNode B) {
if (B == null) {
return false;
}
find(A, B);
return ans;
}
// 比较两个树是否相同
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;
}
// 比较当前节点的最左子树
boolean p1 = compare(root.left, compareNode.left);
// 比较当前节点的右子树
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) {
// 当前节点的值与进行比较的树头节点值相同时,进行比较
boolean compare = compare(root, compareNode);
ans |= compare;
}
// 查找左子树
find(root.left, compareNode);
// 查找右子树
find(root.right, compareNode);
}
}
2.3 测试结果
通过测试

3.总结
- 按照题目要求需要对树进行查找和比较两个过程
- 每查找到一次相同值节点就需要进行一次比较
边栏推荐
- Shortcut keys in the terminal
- Memorabilia of domestic database in June 2022 - ink Sky Wheel
- Kohana database
- 小程序系统更新提示,并强制小程序重启并使用新版本
- Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
- NetXpert XG2帮您解决“布线安装与维护”难题
- AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
- Chapter 3: detailed explanation of class loading process (class life cycle)
- GPS from getting started to giving up (XVIII), multipath effect
- 12、 Start process
猜你喜欢

Classic sql50 questions

GNN, please deepen your network layer~

Assembly and interface technology experiment 5-8259 interrupt experiment

3DMAX assign face map

小常识:保险中的“保全”是什么?

Oracle-控制文件及日志文件的管理

A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely

C#實現水晶報錶綁定數據並實現打印4-條形碼

GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)

How does the uni admin basic framework close the creation of super administrator entries?
随机推荐
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
Applet system update prompt, and force the applet to restart and use the new version
12、 Start process
GPS从入门到放弃(十二)、 多普勒定速
[10:00 public class]: basis and practice of video quality evaluation
Management background --2 Classification list
小程序系统更新提示,并强制小程序重启并使用新版本
Unity3D学习笔记6——GPU实例化(1)
Four data streams of grpc
lora同步字设置
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
每日一题:力扣:225:用队列实现栈
做接口测试都测什么?有哪些通用测试点?
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
Powerful domestic API management tool
GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)
GPS从入门到放弃(十九)、精密星历(sp3格式)
OpenCV VideoCapture. Get() parameter details
Spatial domain and frequency domain image compression of images