当前位置:网站首页>“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
2022-07-26 05:54:00 【学习追求高效率】
“子问题的递归处理”——先看根节点,再看左右子树
判断两棵树是不是相同的树
1. 检查两颗树是否相同
做题逻辑
- 就以根节点 为基础
先判断 两个树的根节点 进行对比- 如果两个根节点 没问题
那么就看 这个根节点的 左右子树 和另一个根节点的左右子树是不是有问题
return false的情况
- 一空一不空 || 两个值不同
return true
- 两个都为空
递归在于 return 递归函数
public boolean isSameTree(TreeNode p, TreeNode q) {
//这里只是判断的 一个为空 一个不为空的情况 。
// 你没有判断 两个都是空 和 两个都不是空的情况
if(p == null && q != null || p != null && q == null) {
return false;
}
if(p == null && q == null) {
return true;
}
if(p.val != q.val) {
return false;
}
//p != null && q != null && p.val == q.val
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
2. 另一颗树的子树(时间复杂度:O(n*m))
public boolean isSameTree(TreeNode p, TreeNode q) {
//这里只是判断的 一个为空 一个不为空的情况 。
// 你没有判断 两个都是空 和 两个都不是空的情况
if(p == null && q != null || p != null && q == null) {
return false;
}
if(p == null && q == null) {
return true;
}
if(p.val != q.val) {
return false;
}
//p != null && q != null && p.val == q.val
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
//时间复杂度:O(n*m) 假设root这棵树的节点个数n subRoot节点个数是m
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if(root == null) return false;
if(isSameTree(root,subRoot)) return true;
if(isSubtree(root.left,subRoot)) return true;
if(isSubtree(root.right,subRoot)) return true;
return false;
}
逻辑:1. 如果树为空 不可能有子树 return flase
if(root == null) return false;
2. 如果 根 与 子树 正好相等 return true
if(isSameTree(root,subRoot)) return true;
3. 如果 根的左 与 子树正好相等 return true
if(isSubtree(root.left,subRoot)) return true;
4. 如果 根的右 与 子树正好相等 return true
if(isSubtree(root.right,subRoot)) return true;
5. if 都没有满足 那么 return false
注意:其实代码在 递归过程中,不仅仅是比较了 根的左和右,(如果没有想要的)还比较的其余的根的左右
原因:代码在递归的过程中,走遍了每个根节点
边栏推荐
- Jincang database kingbasees SQL language reference manual (5. Operators)
- Kingbasees SQL language reference manual of Jincang database (6. Expression)
- Full binary tree / true binary tree / complete binary tree~
- How can programmers improve mental internal friction?
- 金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))
- 知识沉淀一:架构师是做什么?解决了什么问题
- Chapter 1 - Construction of development environment
- Rocbossphp free open source light community system
- You'd better not take this kind of project!
- Motor control column summary
猜你喜欢
![[STM32 series summary] blogger's way to quickly advance STM32 in actual combat (continuous update)](/img/20/bf7d2653aafd35e6588f819d110453.png)
[STM32 series summary] blogger's way to quickly advance STM32 in actual combat (continuous update)

leetcode-aboutString

5-year-old Test Engineer - how to choose the next step?

Efficient, reliable and safe open source solution for serial communication

金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十))

金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))

Redis 官方可视化工具,高颜值,功能真心强大!

You'd better not take this kind of project!

JDBC streaming query and cursor query

Usage and common problems of SIP softphone registered with SIP account
随机推荐
[cloud native] record of feign custom configuration of microservices
【2023杰理科技提前批笔试题】~ 题目及参考答案
FTP experiment and overview
MBA-day28 数的概念-练习题
金仓数据库 KingbaseES SQL 语言参考手册 (7. 条件表达式)
A trick to teach you to easily understand Potter's map
Why can't lpddr completely replace DDR?
Redis持久化-RDB
光量子里程碑:6分钟内解决3854个变量问题
Use of feign (Part 2)
对接微信支付(二)统一下单API
Properties of binary tree~
1.12 basis of Web Development
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’
How students apply for free idea
EM and REM
JS的调用方式与执行顺序
Redis official visualization tool, with high appearance value and powerful functions!
Flex layout
[paper notes] anti packet loss joint coding for network speech steganography


