101. 对称二叉树

​ 给你一个二叉树的根节点 root , 检查它是否轴对称。

示例 1

输入:root = [1,2,2,3,4,4,3]
输出:true

示例 2:

输入:root = [1,2,2,null,3,null,3]
输出:false

提示:

  • 树中节点数目在范围 [1, 1000]
  • -100 <= Node.val <= 100

进阶:你可以运用递归和迭代两种方法解决这个问题吗?

我的方法:老实巴交的层序遍历

​ 使用修改的层序遍历(为空时也添加null),根据队列上次添加的元素个数获得每一层的节点数(包含空节点),然后再判断每一层的节点是否对称(暴击循环或者根据kmp的next数组判断最大公共前后缀),emmm总之就是做得特别麻烦。

递归做法

​ 使用两个遍历同时循环判断,之前看题解做过一次,但是这次还是完全想不到,然后看了一眼想法直接写出来了,还需要多做啊

    public boolean isSymmetric(TreeNode root1, TreeNode root2) {
if(root1 == null && root2 == null) {
return true;
} else if(root1 == null || root2 == null || root1.val != root2.val) {
return false;
} else {
return isSymmetric(root1.left, root2.right)
&& isSymmetric(root1.right, root2.left);
}
} public boolean isSymmetric(TreeNode root) {
if(root == null) {
return true;
}
return isSymmetric(root.left, root.right);
}
非递归写法,类似层序遍历的写法

​ 再次献上我的膝盖,直接看代码可能不是太理解,自己画一下图模拟下队列的过程就会立刻明白了

public boolean isSymmetric(TreeNode root) {
if(root == null) {
return true;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root.left);
queue.add(root.right);
while(!queue.isEmpty()) {
TreeNode leftNode = queue.poll();
TreeNode rightNode = queue.poll();
if(leftNode == null && rightNode == null) {
continue;
}
if(leftNode == null || rightNode == null || leftNode.val != rightNode.val) {
return false;
}
queue.add(leftNode.left);
queue.add(rightNode.right);
queue.add(leftNode.right);
queue.add(rightNode.left);
}
return true;
}

【LeetCode】3.19 对称二叉树的更多相关文章

  1. LeetCode【101. 对称二叉树】

    对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...

  2. Leetcode题目101.对称二叉树(简单)

    题目描述: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null ...

  3. [Leetcode] Symmetric tree 对称二叉树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  4. 【LeetCode】101. 对称二叉树

    题目 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3, ...

  5. LeetCode 101 对称二叉树的几种思路(Python实现)

    对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的.   1   / \ 2   2 / \ / \3 4 4 3 但是下面这个 [1,2,2 ...

  6. Java实现 LeetCode 101 对称二叉树

    101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2 ...

  7. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  8. Leecode刷题之旅-C语言/python-101对称二叉树

    /* * @lc app=leetcode.cn id=101 lang=c * * [101] 对称二叉树 * * https://leetcode-cn.com/problems/symmetri ...

  9. [NOIP2018PJ]对称二叉树

    [NOIP2018PJ]对称二叉树 这个题正常人看到题面难道不是哈希? 乱写了个树哈希... #include<bits/stdc++.h> using namespace std; co ...

  10. C语言递归之对称二叉树

    题目描述 给定一个二叉树,检查它是否是镜像对称的. 示例 二叉树 [1,2,2,3,4,4,3] 是对称的. / \ / \ / \ [1,2,2,null,3,null,3] 则不是镜像对称的. / ...

随机推荐

  1. centos mail 发邮件

    1.安装mailx yum -y install mailx 2. /etc/mail.rc 最后增加邮件配置如 set smtp=smtp.qq.comset smtp-auth=loginset ...

  2. Ubuntu常用环境配置

    gcc sudo apt-get install build-essential update-alternatives 管理python环境 # 添加版本 # update-alternatives ...

  3. K8s+Docker 学习笔记系列

    学前知识 你需要掌握 Linux 的常用命令.你可以通过Linux 教程 来学习相关命令. Docker应用场景 Web 应用的自动化打包和发布. 自动化测试和持续集成.发布. 在服务型环境中部署和调 ...

  4. LINUX下的VSCODE-C/C++配置

    LINUX下的VSCODE-C/C++配置 1.生成默认的任务文件 2.lunch.json,调整"configurations"里的成员,如下 ①添加 "preLaun ...

  5. 前端复习之DOM、BOM

    BOM VS DOM: 1 BOM:浏览器对象模型(API),专门操作浏览器窗口的API 2 没标准! 3 DOM:文档对象模型(API),专门操作网页内容的API 4 可以对网页中任意对象,做任意修 ...

  6. JavaScript argument

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. nginx配置权重,ip_hash....

    nginx为后端web服务器(apache,nginx,tomcat,weblogic)等做反向代理 几台后端web服务器需要考虑文件共享,数据库共享,session共享问题.文件共享可以使用nfs, ...

  8. 转载C#加密方法

    方法一:    //须添加对System.Web的引用     using System.Web.Security;          ...          /// <summary> ...

  9. MQ(部署模式)

    MQ部署模式 1.master-slave部署模式 1)shared filesystem Master-Slave部署方式 主要是通过共享存储目录来实现master和slave的热备,所有的Acti ...

  10. MySQL 导出数据结构 If you don't want to restore GTIDs, pass --set-gtid-purged=OFF.

    应用场景MYSQL导出数据结构 Warning: A partial dump from a server that has GTIDs will by default include the GTI ...