当前位置:网站首页>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;
}
}
边栏推荐
- initial、inherit、unset、revert和all的区别
- Do you feel like you've learned something and forgotten it?
- Quick learning 1.8 front and rear interfaces
- Low code platform international multilingual (I18N) technical solution
- 剑指Offer05. 替换空格
- Day 1 of kotlin learning: simple built-in types of kotlin
- Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
- Tensorflow binary installation & Failure
- Dix règles de travail
- 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?
猜你喜欢
The latest version of blind box mall thinkphp+uniapp
最新版抽奖盲盒运营版
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
Attack and defense world mobile--ph0en1x-100
How to convert a decimal number to binary in swift
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
Xctf mobile--app2 problem solving
idea将web项目打包成war包并部署到服务器上运行
Sword finger offer06 Print linked list from end to end
Social community forum app ultra-high appearance UI interface
随机推荐
elastic_ L01_ summary
Eureka自我保护
并网-低电压穿越与孤岛并存分析
RedHat5 安装Socket5代理服务器
GCN thinking - word2vec directly calculates text classification
Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
Glide 4.6.1 API initial
Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
Swift5.7 extend some to generic parameters
Redhat5 installing socket5 proxy server
Dix règles de travail
阿里大于发送短信(用户微服务--消息微服务)
Nodejs+Express+MySQL实现登陆功能(含验证码)
Idea packages the web project into a war package and deploys it to the server to run
T430 toss and install OS majave 10.14
Ten workplace rules
Xctf mobile--rememberother problem solving
The solution to change the USB flash disk into a space of only 2m
Export the entire Oracle Database
【数据库原理复习题】