当前位置:网站首页>"Recursive processing of subproblems" -- judging whether two trees are the same tree -- and the subtree of another tree
"Recursive processing of subproblems" -- judging whether two trees are the same tree -- and the subtree of another tree
2022-07-26 05:54:00 【Study and pursue high efficiency】
“ Recursive processing of subproblems ”—— Look at the root node first , Look at the left and right subtrees
Judge whether the two trees are the same
- 1. Check whether the two trees are the same
- Question logic
- 2. The subtree of another tree ( Time complexity :O(n*m))
- Logic :1. If the tree is empty There can be no subtree return flase
- 2. If root And subtree Is equal to the return true
- 3. If Left of root And Subtrees are exactly equal return true
- 4. If Right of root And Subtrees are exactly equal return true
- 5. if Not satisfied that return false
- Be careful : In fact, the code is During recursion , It's not just comparison Left and right of the root ,( If there's nothing you want ) Also compare the left and right of the rest of the roots
1. Check whether the two trees are the same
Question logic
- Just take the root node Based on
First judge The root node of the two trees Contrast- If two root nodes That's all right.
Then look at Of this root node Left and right subtrees Is there a problem with the left and right subtrees of another root node
return false The situation of
- Nothing is empty || The two values are different
return true
- Both are empty
Recursion lies in return Recursive function
public boolean isSameTree(TreeNode p, TreeNode q) {
// Here is just judgment One is empty A case that is not empty .
// You have no judgment Both are empty and Neither is empty
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. The subtree of another tree ( Time complexity :O(n*m))
public boolean isSameTree(TreeNode p, TreeNode q) {
// Here is just judgment One is empty A case that is not empty .
// You have no judgment Both are empty and Neither is empty
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);
}
// Time complexity :O(n*m) hypothesis root The number of nodes in this tree n subRoot The number of nodes is 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;
}
Logic :1. If the tree is empty There can be no subtree return flase
if(root == null) return false;
2. If root And subtree Is equal to the return true
if(isSameTree(root,subRoot)) return true;
3. If Left of root And Subtrees are exactly equal return true
if(isSubtree(root.left,subRoot)) return true;
4. If Right of root And Subtrees are exactly equal return true
if(isSubtree(root.right,subRoot)) return true;
5. if Not satisfied that return false
Be careful : In fact, the code is During recursion , It's not just comparison Left and right of the root ,( If there's nothing you want ) Also compare the left and right of the rest of the roots
reason : The code is in the process of recursion , Walked through every root node
边栏推荐
- [free and easy to use] holiday query interface
- JDBC streaming query and cursor query
- idea yml 文件代码不提示解决方案
- [paper notes] anti packet loss joint coding for network speech steganography
- Can you make a JS to get the verification code?
- Solution to slow download speed of vagrant
- Mysql45 speak in simple terms index
- Unity Profiler
- Talking about the practice of software defect management
- QT writing IOT management platform 47 general database settings
猜你喜欢

知识沉淀一:架构师是做什么?解决了什么问题

Day110.尚医通:Gateway集成、医院排班管理:科室列表、根据日期统计数据、排班详情

leetcode-Array

leetcode-Array

Mysql45 speak in simple terms index

NFT in the eyes of blackash: the platform is crying for slaughter, and users send money to the door

日志收集分析平台搭建-1-环境准备

Etcd database source code analysis - cluster membership changes log

对接微信支付(二)统一下单API

Mysql45 talking about logging system: how does an SQL UPDATE statement execute?
随机推荐
[论文笔记] 面向网络语音隐写的抗分组丢失联合编码
Chapter 2 - getting started
如何查看Pod里容器名称
Using easyexcel to import tables to realize batch insertion of xlsx files ----- MySQL of Linux
Processing method of CDC in SDC
Development projects get twice the result with half the effort, a large collection of open source STM32 driver Libraries
【Oracle SQL】计算同比与环比(列转行进行偏移)
How students apply for free idea
Mysql45 talks about transaction isolation: why can't I see it after you change it?
Six sixths -- it's a little late and a little shallow
Redis persistence AOF
[cloud native] record of feign custom configuration of microservices
光量子里程碑:6分钟内解决3854个变量问题
SSH Remote Management
JS的调用方式与执行顺序
Redis持久化-RDB
1.12 basis of Web Development
【(SV && UVM) 笔试面试遇到的知识点】~ phase机制
Dynamic memory management and flexible array
em和rem


