当前位置:网站首页>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;
}
}
边栏推荐
- Kung Fu pays off, and learning is done
- Alibaba is bigger than sending SMS (user microservice - message microservice)
- 自抗扰控制器七-二阶 LADRC-PLL 结构设计
- Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
- Bert running error: attributeerror: module'tensorflow contrib. tpu' has no attribute 'InputPipelineConfig'
- ImportError: No module named examples. tutorials. mnist
- Swift5.7 extend some to generic parameters
- 基于Linu开发的项目视频
- elastic_ L01_ summary
- 剑指Offer07. 重建二叉树
猜你喜欢

【数据挖掘复习题】

TOGAF认证自学宝典V2.0

【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素

Solve the problem of VI opening files with ^m at the end

4. 无线体内纳米网:电磁传播模型和传感器部署要点

Xctf mobile--app1 problem solving

Glide question you cannot start a load for a destroyed activity

Detailed explanation of the most complete constraintlayout in history
![[ManageEngine] the role of IP address scanning](/img/dc/df353da0e93e4d936c39a39493b508.png)
[ManageEngine] the role of IP address scanning

强大的头像制作神器微信小程序
随机推荐
How to convert a decimal number to binary in swift
TOGAF认证自学宝典V2.0
Using swift language features, write a pseudo-random number generator casually
[review questions of database principles]
Method overloading and rewriting
基于同步坐标变换的谐波电流检测
阿里大于发送短信(用户微服务--消息微服务)
(latest version) WiFi distribution multi format + installation framework
自抗扰控制器七-二阶 LADRC-PLL 结构设计
Sword finger offer06 Print linked list from end to end
LeetCode 0556. Next bigger element III - end of step 4
【综合题】【数据库原理】
记录自己vulnhub闯关记录
The latest version of lottery blind box operation version
It feels great to know you learned something, isn‘t it?
Sqoop1.4.4原生增量导入特性探秘
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
[comprehensive question] [Database Principle]
Nodejs+express+mysql realizes login function (including verification code)
Attack and defense world mobile--ph0en1x-100