当前位置:网站首页>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;
}
}
边栏推荐
- Attack and defense world mobile--ph0en1x-100
- Simple use and precautions of kotlin's array array and set list
- Sword finger offer06 Print linked list from end to end
- Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
- Public and private account sending prompt information (user microservice -- message microservice)
- Solve the problem of VI opening files with ^m at the end
- Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
- 01_ Using the concurrent tool class library, is thread safety safe
- Eureka self protection
- 剑指Offer04. 二维数组中的查找【中等】
猜你喜欢

Xctf mobile--app2 problem solving

The latest version of blind box mall thinkphp+uniapp

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

Node.js: express + MySQL的使用

剑指Offer06. 从尾到头打印链表

Low code platform international multilingual (I18N) technical solution

【数据库原理复习题】

Swift bit operation exercise

Implement verification code verification

4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
随机推荐
The solution to change the USB flash disk into a space of only 2m
[exercise 6] [Database Principle]
Nodejs+Express+MySQL实现登陆功能(含验证码)
[embedded] - Introduction to four memory areas
【数据库原理复习题】
Apache Mina Development Manual
基于Linu开发的项目视频
2020-10_ Development experience set
Exploration of sqoop1.4.4 native incremental import feature
How to convert a decimal number to binary in swift
Integer case study of packaging
The latest version of lottery blind box operation version
Swift5.7 扩展 some 到泛型参数
Openstack node address change
社交社区论坛APP超高颜值UI界面
node的ORM使用-Sequelize
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
Lambda expression
Using swift language features, write a pseudo-random number generator casually
Applet wxss introduction