当前位置:网站首页>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;
}
}
边栏推荐
- 启用MemCached的SASL认证
- Swift5.7 extend some to generic parameters
- 基于同步坐标变换的谐波电流检测
- 剑指Offer04. 二维数组中的查找【中等】
- Day 1 of kotlin learning: simple built-in types of kotlin
- Enable SASL authentication for memcached
- 【ManageEngine】IP地址扫描的作用
- Public and private account sending prompt information (user microservice -- message microservice)
- 4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
- Sqoop1.4.4原生增量导入特性探秘
猜你喜欢
Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
Solve the problem of VI opening files with ^m at the end
Sword finger offer09 Implementing queues with two stacks
Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
Analysis of a music player Login Protocol
Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
Swift bit operation exercise
如何在微信小程序中获取用户位置?
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
随机推荐
Analysis of a music player Login Protocol
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
Ten workplace rules
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
双链笔记·思源笔记综合评测:优点、缺点、评价
Eureka self protection
02_ Lock the code, and don't let the "lock" become a worry
Swift5.7 扩展 some 到泛型参数
[ManageEngine] the role of IP address scanning
The latest version of blind box mall thinkphp+uniapp
Oh my Zsh + TMUX installation
Swift return type is a function of function
Low code platform international multilingual (I18N) technical solution
Public and private account sending prompt information (user microservice -- message microservice)
Project video based on Linu development
Use bloc to build a page instance of shutter
How to stand out quickly when you are new to the workplace?
【数据库原理复习题】
最新版抽奖盲盒运营版
[comprehensive question] [Database Principle]