【leetcode-101】 对称二叉树
101. 对称二叉树
(1过)
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
说明:
如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
我的层次遍历:
注意由于下列情况null-3-null-3的存在,和一般的树的层次不一样:
1
/ \
2 2
\ \
3 3
public class SymmetricTree {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
boolean res = true;
while (queue.size() > 0) {
int size = queue.size();
ArrayList<Integer> list = new ArrayList<>();
for (int i=0;i<size;i++) {
TreeNode node = queue.poll();
if (node != null) {
queue.add(node.left);
queue.add(node.right);
list.add(node.val);
} else {
list.add(null);
}
}
if (!isSymmetric(list)){
res = false;
}
}
return res;
}
public boolean isSymmetric(ArrayList<Integer> list) {
boolean flag = true;
for (int i=0;i<list.size()/2;i++) {
if (list.get(i) == null) {
if (list.get(list.size()-1-i) != null)
flag = false;
}
else if (!list.get(i).equals(list.get(list.size()-1-i)))
flag = false;
}
return flag;
}
}
递归:
关键:check(x.left, y.right) && check(x.right, y.left) 左左与右右对称,左右与右左对称
链接:https://www.nowcoder.com/questionTerminal/1b0b7f371eae4204bc4a7570c84c2de1
来源:牛客网
public class Solution {
public static boolean isSymmetric(TreeNode root) {
return check(root, root);
}
public static boolean check(TreeNode x, TreeNode y) {
if(x == null && y == null) return true;
if((x == null && y != null) || (x != null && y == null)) return false;
return x.val == y.val && check(x.left, y.right) && check(x.right, y.left);
}
}
【leetcode-101】 对称二叉树的更多相关文章
- Java实现 LeetCode 101 对称二叉树
101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2 ...
- LeetCode 101 对称二叉树的几种思路(Python实现)
对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \3 4 4 3 但是下面这个 [1,2,2 ...
- LeetCode 101. 对称二叉树(Symmetric Tree)
题目描述 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null, ...
- LeetCode 101.对称二叉树 - JavaScript
题目描述:给定一个二叉树,检查它是否是镜像对称的. 题目分析 下面这种二叉树就是镜像对称的,符合题目要求: 1 / \ 2 2 / \ / \ 3 4 4 3 解法 1:递归检查 根据题目" ...
- LeetCode【101. 对称二叉树】
对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...
- Leetcode题目101.对称二叉树(简单)
题目描述: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null ...
- 【LeetCode】101. 对称二叉树
题目 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3, ...
- 领扣(LeetCode)对称二叉树 个人题解
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...
- 【Leetcode】对称二叉树
递归法 执行用时 :12 ms, 在所有 C++ 提交中击败了43.44%的用户 内存消耗 :14.6 MB, 在所有 C++ 提交中击败了95.56%的用户 /** * Definition for ...
- Leecode刷题之旅-C语言/python-101对称二叉树
/* * @lc app=leetcode.cn id=101 lang=c * * [101] 对称二叉树 * * https://leetcode-cn.com/problems/symmetri ...
随机推荐
- 【CF633D】Fibonacci-ish
题目描述 小y最近迷上了fibonacci数列,他定义了一种数列叫类fibonacci数列: 1.这个数列包含至少\(2\)个元素 2.\(f_0\)和\(f_1\)是任意选取的 3.\(f_{n+2 ...
- 树莓派(Raspberry Pi)使用Shell编写的极简Service
树莓派(Raspberry Pi)运行的系统是基于Debian的,不仅可以运行Shell,还支持systemd和docker,可以编写一个简单的服务,让其在启动时运行,执行一些自动化的操作.这里在Ra ...
- Hdoj 1115.Lifting the Stone 题解
Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...
- django后台密码错误
如果你忘记了设置Django的Admin密码,那么你可以使用createsuperuser来甚至密码,但是如果你忘记了Admin的密码的话,那么就要用Django shell: 1 python ma ...
- linux deb系 rpm系 配置永久IP
rpm: 1.IP a 查看网卡名 ens256 2.uuidgen ens256 生成UUID 3./etc/sysconfig/network-scripts add ifcfg-ens256 4 ...
- Java并发编程-阻塞队列(BlockingQueue)的实现原理
背景:总结JUC下面的阻塞队列的实现,很方便写生产者消费者模式. 常用操作方法 常用的实现类 ArrayBlockingQueue DelayQueue LinkedBlockingQueue Pri ...
- av_seek_frame() 定位为什么不准呢?
初次学习和使用ffmpeg,电脑系统有点老,没办法使用最新版的ffmpeg 3.3,只能从别处下载了一个2.8版的用用,官网提供的历史版本都没有我电脑可用的版本. 花了两天时间学习并写了一个简单的处理 ...
- Good Bye 2018 C. New Year and the Sphere Transmission
传送门 https://www.cnblogs.com/violet-acmer/p/10201535.html 题意: n 个people,编号1~n,按顺时针方向围城一圈: 初始,编号为1的peo ...
- Contest1592 - 2018-2019赛季多校联合新生训练赛第二场(部分题解)
Contest1592 - 2018-2019赛季多校联合新生训练赛第二场 D 10248 修建高楼(模拟优化) H 10252 组装玩具(贪心+二分) D 传送门 题干 题目描述 C 市有一条东西走 ...
- js中两种定时器,setTimeout和setInterval的区别
setTimeout只在指定时间后执行一次,代码如下: <script> //定时器 异步运行 function hello(){ alert("hello&qu ...