一【题目类别】
- 广度优先搜索
二【题目难度】
- 简单
三【题目编号】
- 101.对称二叉树
四【题目描述】
- 给定一个二叉树,检查它是否是镜像对称的。
五【题目示例】
- 示例 1:
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3 - 示例 2:
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
六【题目进阶】
- 你可以运用递归和迭代两种方法解决这个问题吗?
七【解题思路】
- BFS+队列,因为是镜像的,所以小细节在入队列的时候,要对称入队列,如果每次出来的两个结点有一个为空或者两个值不相等,说明就不是对称的二叉树,另外如果每次出来的两个结点同时为空说明叶子节点,那么当前次跳出,进行下一次遍历,其实可以看作将一棵大树分解成一棵棵小树,最后如果没遇到不对称的地方就返回true
八【时间频度】
- 时间复杂度: O ( N ) O(N) O(N)
九【代码实现】
- Java语言版
package BFS;
import java.util.LinkedList;
import java.util.Queue;
public class p101_SymmetricTree {
int val;
p101_SymmetricTree left;
p101_SymmetricTree right;
p101_SymmetricTree(int x){
val = x;
}
public static void main(String[] args) {
// 创建这棵树
p101_SymmetricTree root = new p101_SymmetricTree(1);
p101_SymmetricTree left = new p101_SymmetricTree(2);
p101_SymmetricTree right = new p101_SymmetricTree(2);
p101_SymmetricTree left1 = new p101_SymmetricTree(3);
p101_SymmetricTree left2 = new p101_SymmetricTree(4);
p101_SymmetricTree right1 = new p101_SymmetricTree(4);
p101_SymmetricTree right2 = new p101_SymmetricTree(3);
root.left = left;
root.right = right;
left.left = left1;
left.right = left2;
right.left = right1;
right.right = right2;
boolean res = isSymmetric(root);
if(res){
System.out.println("这棵树是对称的");
}else{
System.out.println("这棵树不是对称的");
}
}
public static boolean isSymmetric(p101_SymmetricTree root) {
// 首先判断是否是空树
if(root==null){
return true;
}
// 利用队列的先进先出特性,类似BFS,后面会比较每个弹出的元素是否相等
Queue<p101_SymmetricTree> temp = new LinkedList<>();
// 因为对称,可以理解为镜像,所以可以理解左右两棵树是否镜像对称
temp.add(root.left);
temp.add(root.right);
while(!temp.isEmpty()){
// 依次弹出每个节点
p101_SymmetricTree t1 = temp.poll();
p101_SymmetricTree t2 = temp.poll();
// 如果两个节点都为空,那么说明已经遍历结束,所以跳出这次循环
if(t1==null && t2 == null){
continue;
}
if(t1==null || t2 == null){
return false;
}
// 如果两个节点的值不相等,直接返回false
if(t1.val != t2.val){
return false;
}
// 能走到这里说明两个节点值相等
// 这里是最重要的,因为是对称,所以入队列的次序非常重要
// 我们首先将t1的左节点加入,再将t2的右节点加入,然后将t1的右节点加入,再将t2的左节点加入
temp.add(t1.left);
temp.add(t2.right);
temp.add(t1.right);
temp.add(t2.left);
}
// 跳出while循环后说明每一个节点都遍历到了,而且是对称的,所以返回true
return true;
}
}
- C语言版
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*注:1为true,0为false*/
typedef int bool;
/*定义树*/
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
bool isSymmetric(struct TreeNode* root)
{
if (root == NULL)
{
return 1;
}
return fun(root->left, root->right);
}
bool fun(struct TreeNode* l_root, struct TreeNode* r_root)
{
if (l_root == NULL && r_root == NULL)
{
return 1;
}
if (l_root == NULL || r_root == NULL)
{
return 0;
}
return (l_root->val == r_root->val) && fun(l_root->left, r_root->right) && fun(l_root->right, r_root->left);
}
/*主函数内容省略,上面的算法是核心*/
十【提交结果】
-
Java语言版
-
C语言版