当前位置:网站首页>Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
2022-07-03 12:50:00 【Mcc_ mingchao】


The symmetry mentioned here is axial symmetry , in other words , The left child of the left subtree should be the same as the right child of the right subtree , Left subtree right child is the same as right subtree left child .
Here is the recursive method :
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return deep(root.left,root.right);
}
public boolean deep(TreeNode left,TreeNode right){
if(left==null&&right==null){
return true;// Empty is true
}if(left==null||right==null){
return false;// If one is empty, it is false
}if(left.val!=right.val){
return false;// Inequality is false
}
return deep(left.left,right.right)&&deep(left.right,right.left);// recursive Left subtree left child and right subtree right child comparison , Left subtree right child and right subtree left child comparison .
}
}Iterative method :
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null || (root.left==null && root.right==null)) {
return true;
}
// Save node with queue
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
// Put the left and right children of the root node in the queue
queue.add(root.left);
queue.add(root.right);
while(queue.size()>0) {
// Take two nodes out of the queue , Then compare the two nodes
TreeNode left = queue.removeFirst();
TreeNode right = queue.removeFirst();
// If both nodes are empty, continue the loop , If one of them is empty, it returns false
if(left==null && right==null) {
continue;
}
if(left==null || right==null) {
return false;
}
if(left.val!=right.val) {
return false;
}
// The left child of the left node , Put the right child of the right node into the queue
queue.add(left.left);
queue.add(right.right);
// The right child of the left node , The left child of the right node is put in the queue
queue.add(left.right);
queue.add(right.left);
}
return true;
}
}
边栏推荐
- Powerful avatar making artifact wechat applet
- Kung Fu pays off, and learning is done
- Airflow installation jump pit
- Sword finger offer10- I. Fibonacci sequence
- JVM memory model
- 基于同步坐标变换的谐波电流检测
- Swift5.7 extend some to generic parameters
- Node.js: express + MySQL的使用
- Sword finger offer06 Print linked list from end to end
- 阿里大于发送短信(用户微服务--消息微服务)
猜你喜欢

剑指Offer04. 二维数组中的查找【中等】

【数据挖掘复习题】

Alibaba is bigger than sending SMS (user microservice - message microservice)

Glide question you cannot start a load for a destroyed activity

电压环对 PFC 系统性能影响分析

自抗扰控制器七-二阶 LADRC-PLL 结构设计

强大的头像制作神器微信小程序

Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?

The latest version of lottery blind box operation version

Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
随机推荐
flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
[exercice 7] [principe de la base de données]
4. 无线体内纳米网:电磁传播模型和传感器部署要点
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
[embedded] - Introduction to four memory areas
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
Attack and defense world mobile--ph0en1x-100
阿里 & 蚂蚁自研 IDE
Integer case study of packaging
剑指Offer07. 重建二叉树
Togaf certification self-study classic v2.0
基于同步坐标变换的谐波电流检测
(最新版) Wifi分销多开版+安装框架
Swift return type is a function of function
studio All flavors must now belong to a named flavor dimension. Learn more
Method overloading and rewriting
Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
[ManageEngine] the role of IP address scanning
Dix règles de travail
【习题六】【数据库原理】