当前位置:网站首页>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;
}
}
边栏推荐
- Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
- elastic_ L01_ summary
- Enter the length of three sides of the triangle through the user, and calculate the area of the triangle, where the length is a real number
- Record your vulnhub breakthrough record
- 启用MemCached的SASL认证
- Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
- 【数据挖掘复习题】
- 2020-11_ Technical experience set
- [embedded] - Introduction to four memory areas
- Analysis of a music player Login Protocol
猜你喜欢

Swift bit operation exercise

自抗扰控制器七-二阶 LADRC-PLL 结构设计
![[download attached] password acquisition tool lazagne installation and use](/img/21/eccf87ad9946d4177b600d96e17322.png)
[download attached] password acquisition tool lazagne installation and use

阿里 & 蚂蚁自研 IDE

Method overloading and rewriting

最新版盲盒商城thinkphp+uniapp

基于同步坐标变换的谐波电流检测

initial、inherit、unset、revert和all的区别

(latest version) WiFi distribution multi format + installation framework

Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
随机推荐
写一个简单的nodejs脚本
十条职场规则
Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
Sword finger offer09 Implementing queues with two stacks
基于Linu开发的项目视频
The foreground uses RSA asymmetric security to encrypt user information
The future of cloud computing cloud native
Xctf mobile--app3 problem solving
Nodejs+express+mysql realizes login function (including verification code)
The latest version of blind box mall thinkphp+uniapp
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
Swift5.7 extend some to generic parameters
Xctf mobile--rememberother problem solving
node的ORM使用-Sequelize
Oh my Zsh + TMUX installation
公纵号发送提示信息(用户微服务--消息微服务)
Swift5.7 扩展 some 到泛型参数
Using swift language features, write a pseudo-random number generator casually
Sword finger offer04 Search in two-dimensional array [medium]
It feels great to know you learned something, isn‘t it?