当前位置:网站首页>Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
2022-04-23 04:47:00 【wyplj_ sir】
subject :
Please implement a function , Used to judge whether a binary tree is symmetrical . Be careful , If a binary tree has the same image as this binary tree , Define it as symmetric .
for example , Binary tree [1,2,2,3,4,4,3] It's symmetrical .
1
/ \
2 2
/ \ / \
3 4 4 3
But the next one [1,2,2,null,3,null,3] It's not mirror symmetric :
1
/ \
2 2
\ \
3 3
answer :
Solution 1 :
recursive , The code is as follows :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null){
return true;
}
return compare(root.left, root.right);
}
public boolean compare(TreeNode A, TreeNode B){
if(A == null && B == null){
return true;
}
if(A == null || B == null){
return false;
}
return A.val == B.val && compare(A.left, B.right) && compare(A.right, B.left);
}
}
Solution 2 :
iteration , The code is as follows :
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
if(pRoot==null){
return true;
}
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(pRoot.left);
queue.add(pRoot.right);
while(!queue.isEmpty()){
TreeNode node1 = queue.poll();
TreeNode node2 = queue.poll();
if(node1==null&&node2==null){
continue;
}
if(node1==null||node2==null){
return false;
}
if(node1.val==node2.val){
queue.add(node1.left);
queue.add(node2.right);
queue.add(node1.right);
queue.add(node2.left);
}else{
return false;
}
}
return true;
}
}
Reference link :https://leetcode-cn.com/problems/symmetric-tree/solution/dui-cheng-er-cha-shu-by-leetcode/
版权声明
本文为[wyplj_ sir]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220555179136.html
边栏推荐
- 简单的拖拽物体到物品栏
- Solutions to the failure of sqoop connection to MySQL
- Unity3D 实用技巧 - 理论知识库(一)
- Record your own dataset with d435i, run orbslam2 and build a dense point cloud
- IEEE Transactions on industrial information (TII)
- Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
- General enumeration constant class
- Create VPC in AWS console (no plate)
- Learning Android from scratch -- Introduction
- What's the difference between error and exception
猜你喜欢
Supplement 14: cmake practice project notes (to be continued 4 / 22)
Painless upgrade of pixel series
Key points of AWS eks deployment and differences between console and eksctl creation
用LCR表完美测试无线充电系统中的线圈
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
Windows remote connection to redis
Innovation training (IV) preliminary preparation - server
Innovation training (V) configuration information
C language: spoof games
520. Detect capital letters
随机推荐
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
View analysis of scenic spots in ArcGIS
Unity rawimage background seamlessly connected mobile
Recommended scheme of national manufactured electronic components for intelligent electronic scales
Raspberry pie + opencv + opencv -- face detection ------- environment construction
Unity3D 实用技巧 - 理论知识库(一)
PHP 统计指定文件夹下文件的数量
Small volume Schottky diode compatible with nsr20f30nxt5g
MySQL queries users logged in for at least N consecutive days
Mysql, binlog log query
unity摄像机旋转带有滑动效果(自转)
Innovation training (10)
redis和mysql区别
What is a blocking queue? What is the implementation principle of blocking queue? How to use blocking queue to implement producer consumer model?
Wechat payment function
Leetcode008 -- implement strstr() function
Leetcode002 -- inverts the numeric portion of a signed integer
FAQ of foreign lead and alliance Manager
Leetcode005 -- delete duplicate elements in the array in place
Com alibaba. Common methods of fastjson