当前位置:网站首页>Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
2022-07-25 18:11:00 【Grilled little fat sheep with charcoal...】
101. Symmetric binary tree 
title : recursive
The recursive trilogy is introduced below :
Determine the parameters and return values of recursive functions
Because we want to compare whether the two subtrees of the root node flip each other , Then judge whether the tree is a symmetric tree , So the two trees to compare , Parameters are also left subtree nodes and right subtree nodes . The return value is naturally boolean type . The code is as follows : bool compare(TreeNode left, TreeNode right)Determine termination conditions
To compare two nodes with different values , First of all, we should make clear the situation that the two nodes are empty ! Otherwise, the null pointer will be operated when comparing values later . When the node is empty, there are :( Note that we are not comparing the left child and the right child , So I call it the left node and the right node ) 1. Left and right are empty , symmetry , return true 2. Left node is empty , The right node is not empty , Asymmetry ,return false 3. Left not empty , Right is empty. , Asymmetry return false 4. Left and right are not empty , Compare node values , If it's different, it's just return false At this time, the left and right nodes are not empty , And the values are different, we also deal with . The code is as follows : if (left == NULL && right != NULL) return true; if (left== NULL || right == NULL) return false; if (left->val != right->val) return false; Let's rule out all the above situations , The rest is Left and right nodes are not empty , And the same value .Determine the logic of monolayer recursion
At this point, we enter the logic of single-layer recursion , The logic of single-layer recursion is to deal with Left and right nodes are not empty , And the same value . Compare whether the outside of the binary tree is symmetrical : The left child of the left node is passed in , The right child of the right node . Compare whether the internal measurement is symmetrical , Pass in the right child of the left node , The left child of the right node . If both sides are symmetrical, return to true , If one side is asymmetric, it returns false . The code is as follows : bool outside = compare(left->left, right->right); // The left subtree : Left 、 Right subtree : Right bool inside = compare(left->right, right->left); // The left subtree : Right 、 Right subtree : Left bool isSame = outside && inside; // The left subtree : in 、 Right subtree : in ( Logical processing ) return isSame;
Code implementation :
class Solution {
public boolean isSymmetric(TreeNode root) {
return compare(root.left, root.right);
}
private boolean compare(TreeNode left, TreeNode right){
if(left == null && right == null){
return true;
}
if(left == null || right == null){
return false;
}
if(left.val != right.val){
return false;
}
// Compare the outside of the tree
boolean outside = compare(left.left, right.right);
// Compare the inside of the tree
boolean inside = compare(left.right, right.left);
return outside && inside;
}
}
100. Same tree 
Code implementation :
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
return compare(p, q);
}
private boolean compare(TreeNode p, TreeNode q){
if(left == null && right == null){
return true;
}
if(left == null || right == null){
return false;
}
if(left.val != right.val){
return false;
}
// Compare the left side of the two trees
boolean outCompare = compare(p.left, q.left); // A tree compares the outside and the inside , The two trees are compared on the same side
// Compare the right side of the two trees
boolean inCompare = compare(p.right, q.right);
return outCompare && inCompare;
}
}
572. The subtree of another tree 
Code implementation ( Sequence traversal + recursive ):
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
// Declare a secondary queue
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
while(len > 0){
TreeNode tempNode = queue.poll();
if(compare(tempNode, subRoot)){
// Compare compare(tempNode, subRoot) Not compare(root, subRoot)
return true;
}
if(tempNode.left != null) queue.offer(tempNode.left);
if(tempNode.right != null) queue.offer(tempNode.right);
len--;
}
}
return false;
}
public boolean compare(TreeNode p, TreeNode q){
if(p == null && q == null){
return true;
}
if(p == null || q == null){
return false;
}
if(p.val != q.val){
return false;
}
return compare(p.left, q.left) && compare(p.right, q.right);
}
}
边栏推荐
- 绘制pdf表格 (二) 通过itext实现在pdf中绘制excel表格样式设置中文字体、水印、logo、页眉、页码
- Redis source code and design analysis -- 15. RDB persistence mechanism
- Mock服务moco系列(一)- 简介、第一个Demo、Get请求、Post请求
- 虚拟偶像代言产品出问题谁负责?
- What scenarios have rust, which is becoming more and more mature, applied?
- How to read a Book
- [MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?
- Sorting also needs to know the information and linked list
- TME2022校园招聘后台开发/运营开发/业务运维/应用开发笔试(I)编程题的一点自我分析
- Cloud VR: the next step of virtual reality specialization
猜你喜欢

如何判断静态代码质量分析工具的性能?这五大因素必须考虑

Introduction to cloud XR and development opportunities of cloud XR in 5g Era

What is the relationship between cloud fluidization and cloud desktop

Redis source code and design analysis -- 15. RDB persistence mechanism
![[MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?](/img/1f/a2d50ec6bc97d52c1e7566a42e564b.png)
[MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?

Could not stop Cortex-M device! Please check the JTAG cable solution

Li Kai: the interesting and cutting-edge audio and video industry has always attracted me

Update 3dcat real time cloud rendering V2.1.2 release

使用sqldeveloper连接mysql

PageHelper can also be combined with lambda expressions to achieve concise paging encapsulation
随机推荐
OV7725 yuv 640*[email protected] 配置文件
Update 3dcat real time cloud rendering V2.1.2 release
C语言 cJSON库的使用
What scenarios have rust, which is becoming more and more mature, applied?
直击考点:PMP考试中常见敏捷知识点汇总
How many points did NPDP pass? How to pass with high scores?
使用sqldeveloper连接mysql
Creation of unity Bezier curve
Which real-time gold trading platform is reliable and safe?
Bl602 development environment setup
ORB_SLAM3复现——上篇
Cloud XR面临的问题以及Cloud XR主要应用场景
Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"
mysql的小数number类型select之后丢失了前面的0
nodejs 简单例子程序之express
云流化和云桌面有什么关系
Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?
SLA 、SLO & SLI
Ch582 ble 5.0 uses Le coded broadcast and connection
STM8S003F3 uart的使用