当前位置:网站首页>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;
}
}
边栏推荐
- Social community forum app ultra-high appearance UI interface
- Low code platform international multilingual (I18N) technical solution
- Swift Error Handling
- Xctf mobile--app2 problem solving
- 写一个简单的nodejs脚本
- Tensorflow binary installation & Failure
- Is it safe to open an account for online stock speculation? Who can answer
- Is it OK to open an account for online stock speculation? Is the fund safe?
- [judgment question] [short answer question] [Database Principle]
- ImportError: No module named examples. tutorials. mnist
猜你喜欢

社交社区论坛APP超高颜值UI界面
![[review questions of database principles]](/img/c3/81d192a40bcc4f5d72fcbe76c708bb.png)
[review questions of database principles]

How to get user location in wechat applet?

剑指Offer10- I. 斐波那契数列

Powerful avatar making artifact wechat applet

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

Swift bit operation exercise

公纵号发送提示信息(用户微服务--消息微服务)

Low code platform international multilingual (I18N) technical solution

GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
随机推荐
[exercise 6] [Database Principle]
Detailed explanation of the most complete constraintlayout in history
Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
node的ORM使用-Sequelize
启用MemCached的SASL认证
低代码平台国际化多语言(i18n)技术方案
The latest version of blind box mall thinkphp+uniapp
ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍
Using swift language features, write a pseudo-random number generator casually
I'm too lazy to write more than one character
OpenStack节点地址改变
The foreground uses RSA asymmetric security to encrypt user information
Simple use and precautions of kotlin's array array and set list
(latest version) WiFi distribution multi format + installation framework
剑指Offer05. 替换空格
Tianyi ty1208-z brush machine detailed tutorial (free to remove)
(最新版) Wifi分销多开版+安装框架
RedHat5 安装Socket5代理服务器
阿里大于发送短信(用户微服务--消息微服务)