当前位置:网站首页>“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
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
注意:其实代码在 递归过程中,不仅仅是比较了 根的左和右,(如果没有想要的)还比较的其余的根的左右
原因:代码在递归的过程中,走遍了每个根节点
边栏推荐
- 金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
- 金仓数据库 KingbaseES SQL 语言参考手册 (10. 查询和子查询)
- Redis主从复制
- Who is responsible for the problems of virtual idol endorsement products? And listen to the lawyer's analysis
- The idea YML file code does not prompt the solution
- Full binary tree / true binary tree / complete binary tree~
- Redis发布订阅
- leetcode-Array
- 金仓数据库 KingbaseES SQL 语言参考手册 (5. 操作符)
- Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
猜你喜欢

为什么LPDDR不能完全代替DDR?

Unity2d animator cannot create transition

Use latex to typeset multiple-choice test paper

leetcode-Array

LNMP architecture

满二叉树 / 真二叉树 / 完全二叉树 ~

Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))

ES Cluster in Red status: what about write & delete operations?

102. (cesium chapter) cesium road streamer

知识沉淀一:架构师是做什么?解决了什么问题
随机推荐
调试利器!一款轻量级日志库 log.c
Development projects get twice the result with half the effort, a large collection of open source STM32 driver Libraries
Kingbasees SQL language reference manual of Jincang database (6. Expression)
Detailed explanation of the whole process of coding's pressure measurement operation
Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
A trick to teach you to easily understand Potter's map
Redis主从复制
How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects
Three graduates, three years of experience in embedded software
Establishment of log collection and analysis platform-1-environment preparation
知识沉淀一:架构师是做什么?解决了什么问题
K. Link with Bracket Sequence I dp
leetcode-Array
SSH Remote Management
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’
金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
金仓数据库 KingbaseES SQL 语言参考手册 (11. SQL语句:ABORT 到 ALTER INDEX)
顺序查找,折半查找,分块查找 ~
Processing method of CDC in SDC
Blurring of unity pixel painting


