当前位置:网站首页>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;
}
}
边栏推荐
- 02_ Lock the code, and don't let the "lock" become a worry
- Project video based on Linu development
- Sword finger offer09 Implementing queues with two stacks
- Node.js: express + MySQL的使用
- initial、inherit、unset、revert和all的区别
- TOGAF认证自学宝典V2.0
- Xctf mobile--app3 problem solving
- 启用MemCached的SASL认证
- How to convert a decimal number to binary in swift
- CNN MNIST handwriting recognition
猜你喜欢

低代码平台国际化多语言(i18n)技术方案

剑指Offer09. 用两个栈实现队列

【数据库原理复习题】

Detailed explanation of the most complete constraintlayout in history

Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
![[comprehensive question] [Database Principle]](/img/d7/8c51306bb390e0383a017d9097e1e5.png)
[comprehensive question] [Database Principle]

Cloud Computing future - native Cloud

LeetCode 0556. Next bigger element III - end of step 4

Public and private account sending prompt information (user microservice -- message microservice)

With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
随机推荐
[review questions of database principles]
flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
十条职场规则
【习题六】【数据库原理】
Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
Ten workplace rules
Oh my Zsh + TMUX installation
RedHat5 安装Socket5代理服务器
【综合题】【数据库原理】
[problem exploration and solution of one or more filters or listeners failing to start]
Write a simple nodejs script
Eureka self protection
[exercice 7] [principe de la base de données]
剑指Offer03. 数组中重复的数字【简单】
[ManageEngine] the role of IP address scanning
Applet wxss introduction
4. 无线体内纳米网:电磁传播模型和传感器部署要点
The latest version of lottery blind box operation version
Sword finger offer05 Replace spaces
Quickly learn member inner classes and local inner classes