当前位置:网站首页>Sword finger offer 28. symmetric binary tree
Sword finger offer 28. symmetric binary tree
2022-07-26 02:12:00 【ThE wAlkIng D】
Title Description

Problem analysis
Ontology uses queues to do , First, judge whether the subtree is empty
Then put left and right , Determine whether the element of the queue is empty , Four conditions must be considered .
Then put the left side of the left subtree The right side of the right subtree is put into the queue in turn ( Put in pairs )
Code instance
class Solution {
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> deque = new LinkedList<>();
if(root == null){
return true;
}
deque.offer(root.left);
deque.offer(root.right);
while (!deque.isEmpty()) {
TreeNode leftNode = deque.poll();
TreeNode rightNode = deque.poll();
if (leftNode == null && rightNode == null) {
continue;
}// If all are empty, judge , Space symmetry continues
// if (leftNode == null && rightNode != null) {
// return false;
// }
// if (leftNode != null && rightNode == null) {
// return false;
// }
// if (leftNode.val != rightNode.val) {
// return false;
// }
// The above three judgment conditions are combined
if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) {
return false;
}
// Here is the sequence and use Deque Different
deque.offer(leftNode.left);
deque.offer(rightNode.right);
deque.offer(leftNode.right);
deque.offer(rightNode.left);
}
return true;
}
}
边栏推荐
- I.MX6UL核心模块使用连载-eMMC读写测试 (四)
- BGP knowledge points summary
- The slow loading of the first entry page of vite local operation
- Characteristics and determination of neuraminidase from Clostridium perfringens in Worthington
- How to install opengauss manually (non om mode)
- C# 迭代器的实现
- I.MX6UL核心模块使用连载-WIFI测试 (八)
- MPLS知识点
- Why does the debugger display the wrong function
- DQN Pytorch示例
猜你喜欢

图解B+树的插入过程
![[leetcode] 32. Longest valid bracket](/img/5e/45bb0b1ca3d9e429c6c5cf5c4c93ae.png)
[leetcode] 32. Longest valid bracket

SQLyog数据导入导出图文教程

Ggplot2 learning summary
![[2021] [paper notes] 6G technology vision - otfs modulation technology](/img/50/577ad05bc16e80d1c68eec7b6da988.png)
[2021] [paper notes] 6G technology vision - otfs modulation technology

DialogRPT-Dialog Ranking Pretrained Transformers

I.MX6UL核心模块使用连载-WIFI测试 (八)

1. Mx6ul core module serial USB interface test (VI)

MySQL transaction isolation level

# Dest0g3 520迎新赛(更新中)
随机推荐
[C language brush leetcode] 1604. Warn people who use the same employee card more than or equal to three times within an hour (m)
Bitmap这个“内存刺客”你要小心~
Alibaba cloud redis development specification
[Android development IOS series] Language: swift vs kotlin
Error reporting caused by local warehouse
Adruino 基础实验学习(一)
The slow loading of the first entry page of vite local operation
ggplot2学习总结
一种MCU事件型驱动C框架
1. Mx6ul core module serial use - touch screen calibration (IX)
2022-07-17
I.MX6UL核心模块使用连载-TF卡读写测试 (五)
[paper reading] coat: CO scale conv attentional image transformers
DialogRPT-Dialog Ranking Pretrained Transformers
Qt程序美化之样式表的使用方法,Qt使用图片作为背景与控件透明化,Qt自定义按钮样式
What is the difference between for... In... And for... Of
Characteristics and determination of neuraminidase from Clostridium perfringens in Worthington
National standard gb28181 protocol video platform easygbs message pop-up mode optimization
c# 单元测试
I.MX6UL核心模块使用连载-CAN、蜂鸣器测试 (十一)