当前位置:网站首页>"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
边栏推荐
- [personal summary] end of July 24, 2022
- Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
- 金仓数据库 KingbaseES SQL 语言参考手册 (10. 查询和子查询)
- Mba-day29 arithmetic - preliminary understanding of absolute value
- Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
- Establishment of log collection and analysis platform-1-environment preparation
- 顺序查找,折半查找,分块查找 ~
- Use latex to typeset multiple-choice test paper
- Introduction to Chinese text error correction task
- Lightweight MCU command line interaction project, all open source
猜你喜欢

Development projects get twice the result with half the effort, a large collection of open source STM32 driver Libraries

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

Benji Bananas 开启第二季边玩边赚奖励活动,支持使用多张 Benji 通行证!

Another open source artifact, worth collecting and learning!

unity 像素画导入模糊问题

The idea YML file code does not prompt the solution

Redis official visualization tool, with high appearance value and powerful functions!

漫谈软件缺陷管理的实践

1.12 basis of Web Development

Blurring of unity pixel painting
随机推荐
[free and easy to use] holiday query interface
Autumn move - Preparation Plan
Mba-day28 concept of number - exercise questions
Efficient, reliable and safe open source solution for serial communication
520送什么?DIY一个高颜值RGB时钟,女生看了都想要
Flex layout
Lemon class automatic learning after all
Jupiter notebook shortcut key
Talking about the practice of software defect management
知识沉淀一:架构师是做什么?解决了什么问题
Development projects get twice the result with half the effort, a large collection of open source STM32 driver Libraries
金仓数据库 KingbaseES SQL 语言参考手册 (5. 操作符)
卸载手机自带APP的操作步骤
The idea YML file code does not prompt the solution
【2023杰理科技提前批笔试题】~ 题目及参考答案
leetcode-aboutString
Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
Redis persistence AOF
Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
金仓数据库 KingbaseES SQL 语言参考手册 (9. 常见DDL子句)


