当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

Characteristics and determination of neuraminidase from Clostridium perfringens in Worthington

1. Mx6ul core module uses serial EMMC read / write test (IV)

主键B+ Tree,二级索引B+ Tree及对应的查询过程分析

Implementation of Ti am335x industrial control module network and file system nfs

c# 单元测试

微信小程序解密并拆包获取源码教程

Obsidian mobile PC segment synchronization

Are you still using ==0 null equal to judge null values? How much do you know about isempty and isblank

Advantages of composition API

I.MX6UL核心模块使用连载-nand flash读写测试 (三)
随机推荐
c# 单元测试
Design and driver transplantation of matrix keyboard circuit of Ti am335x industrial control module
【2020】【论文笔记】磁控溅射法生长Bi2Te3/CoFeB双层异质结——
[C language brush leetcode] 146. LRU cache (m)
Activiti workflow gateway
1. Mx6ul core module serial Ethernet test (VII)
Ti AM335X工控模块矩阵键盘电路的设计与驱动移植
These practical security browser plug-ins improve your efficiency
prometheus+blackbox-exporter+grafana 监控服务器端口及url地址
How to install opengauss manually (non om mode)
2. Login - verification code function and saving login status
Bitmap这个“内存刺客”你要小心~
Postman报Json序列化错误
Relationship between HTC mobile official solution, s-on/s-off and super CID
I.MX6UL核心模块使用连载-Iot-6ULX核心模块简要介绍 (一)
I.MX6UL核心模块使用连载-CAN、蜂鸣器测试 (十一)
Ggplot2 learning summary
I.MX6UL核心模块使用连载-nand flash读写测试 (三)
1. Mx6ul core module uses serial can and buzzer test (XI)
SQL手工盲注、报错注入