当前位置:网站首页>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
- redis和mysql区别
- La caméra Unity tourne avec la souris
- Learning Android from scratch -- Introduction
- Unity RawImage背景无缝连接移动
- Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000
- MySQL queries users logged in for at least N consecutive days
- Innovation training (V) configuration information
- leetcode005--原地删除数组中的重复元素
- The programmer starts the required application with one click of window bat
猜你喜欢
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
Learning Android from scratch -- Introduction
Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000
C language: spoof games
PIP3 installation requests Library - the most complete pit sorting
Practice and exploration of knowledge map visualization technology in meituan
Mysql50 basic exercises
Improving 3D object detection with channel wise transformer
Repair of self calibration SPC failure of Tektronix oscilloscope dpo3054
MySQL queries users logged in for at least N consecutive days
随机推荐
Spark small case - RDD, spark SQL
Small volume Schottky diode compatible with nsr20f30nxt5g
Innovation training (IX) integration
[pytoch foundation] torch Split() usage
Innovation training (IV) preliminary preparation - server
The last day of 2021 is the year of harvest.
Practice and exploration of knowledge map visualization technology in meituan
What is a blocking queue? What is the implementation principle of blocking queue? How to use blocking queue to implement producer consumer model?
Jetpack -- lifecycle usage and source code analysis
List&lt; Map&gt; Replication: light copy and deep copy
Gets all dates between two times
Unity RawImage背景无缝连接移动
JS détermine si la chaîne de nombres contient des caractères
383. Ransom letter
Getprop property
View analysis of scenic spots in ArcGIS
Pixel mobile phone brick rescue tutorial
CLion+OpenCV identify ID number - detect ID number
Leetcode002 -- inverts the numeric portion of a signed integer
Excel protects worksheets and workbooks from damage