当前位置:网站首页>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);
}
}
边栏推荐
- Introduction to cloud XR and development opportunities of cloud XR in 5g Era
- Nineteen year old summary
- 云VR:虚拟现实专业化的下一步
- Wu Enda's machine learning programming operation cannot be suspended pause problem solved
- Could not stop Cortex-M device! please check the JTAG cable的解决办法
- SLA 、SLO & SLI
- Use of C language cjson Library
- Basic operation of bidirectional linked list
- Oracle使用impdp导入报错:ORA-39001: 参数值无效 ORA-39000: 转储文件说明错误 ORA-39088: 文件名不能包含路径说明
- Cloud VR: the next step of virtual reality specialization
猜你喜欢

What scenarios have rust, which is becoming more and more mature, applied?

绘制pdf表格 (一) 通过itext实现在pdf中绘制excel表格样式并且实现下载(支持中文字体)

Update 3dcat real time cloud rendering V2.1.2 release

Redis source code and design analysis -- 16. AOF persistence mechanism

Express of nodejs simple example program
![Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?](/img/1f/a2d50ec6bc97d52c1e7566a42e564b.png)
Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?

Keil5 "loading PDSC debug description failed for STMicroelectronics stm32hxxxxxxx" solution

SLA 、SLO & SLI

Related operations of binary tree

Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法
随机推荐
Sequential storage structure, chain storage structure and implementation of stack
How to read a Book
Introduction to cloud XR and development opportunities of cloud XR in 5g Era
[MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?
Redis source code and design analysis -- 15. RDB persistence mechanism
TESTNG中的并发测试invocationCount, threadPoolSize, timeOut的使用
绘制pdf表格 (二) 通过itext实现在pdf中绘制excel表格样式设置中文字体、水印、logo、页眉、页码
Use of LCD screen of kendryte k210 on FreeRTOS
3DCAT v2.1.3新版本发布,这三大功能更新你不容错过!
408第二章线性表
图的相关操作
BiSeNet v1
MySQL数据库常用命令
How many points did NPDP pass? How to pass with high scores?
Recommend a qinheng Bluetooth reference blog
文件基础知识
专访即构科技李凯:音视频的有趣、行业前沿一直吸引着我
srec_ Use of common cat parameters
OV7725 yuv 640*[email protected] 配置文件
二叉树的相关操作