当前位置:网站首页>"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
边栏推荐
- 金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
- 1.12 basis of Web Development
- Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
- The refurbishment and counterfeiting of chips have made people feel numb
- Mysql45 talking about global lock, table lock and row lock
- ES Cluster in Red status: what about write & delete operations?
- Is the transaction in mysql45 isolated or not?
- 1.12 Web开发基础
- Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
- JS的调用方式与执行顺序
猜你喜欢

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

102. (cesium chapter) cesium road streamer

1.12 Web开发基础

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

A trick to teach you to easily understand Potter's map

Debugging sharp weapon! A lightweight log library log.c

Is the transaction in mysql45 isolated or not?

Benji Banas launched the second season of earn while playing bonus activities, supporting the use of multiple Benji passes!

MBA-day28 数的概念-练习题

How students apply for free idea
随机推荐
520送什么?DIY一个高颜值RGB时钟,女生看了都想要
EM and REM
软件测试面试题全网独家没有之一的资深测试工程师面试题集锦
满二叉树 / 真二叉树 / 完全二叉树 ~
Development projects get twice the result with half the effort, a large collection of open source STM32 driver Libraries
Use of feign (Part 2)
金仓数据库 KingbaseES SQL 语言参考手册 (5. 操作符)
sdc中对cdc的处理方式
Unity2D 动画器无法 创建过渡
Kingbasees SQL language reference manual of Jincang database (8. Function (10))
Binary sort tree (BST)~
招标信息获取
Project topic selection reference
Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
I also found excellent software and hardware projects, all open source
金仓数据库 KingbaseES SQL 语言参考手册 (10. 查询和子查询)
ES Cluster in Red status: what about write & delete operations?
How are masters refined?
为什么LPDDR不能完全代替DDR?
[paper notes] anti packet loss joint coding for network speech steganography


