[leetcode]101. Symmetric Tree对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
题目
给定二叉树,判断其是否左右对称
思路
DFS
代码
/*
Time:O(n),
Space: O(logn) 即 O(h) 树的deepest hight
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return isSymmetric(root.left, root.right);
}
private static boolean isSymmetric(TreeNode p, TreeNode q) {
if (p == null && q == null) return true; // 终止条件
if (p == null || q == null) return false; // 终止条件
return p.val == q.val // 三方合并
&& isSymmetric(p.left, q.right)
&& isSymmetric(p.right, q.left);
}
}
[leetcode]101. Symmetric Tree对称树的更多相关文章
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- LeetCode 101. Symmetric Tree(镜像树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】Symmetric Tree(对称二叉树)
这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...
- leetcode 101 Symmetric Tree ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- sql中的函数
函数类别 作用 聚合函数 执行的操作是将多个值合并为一个值.例如 COUNT.SUM.MIN 和 MAX. 配置函数 是一种标量函数,可返回有关配置设置的信息. 转换函数 将值从一种数据类型转换为另一 ...
- redis 学习笔记3(哨兵模式下分布式锁的实现以及全局唯一id的生成)
redis实现分布式锁和全局唯一id应该是较为常见的应用. 实现基于redis的setNX,以及incr命令.还是比较简单的! 搭建环境以及配置好sping整合,做了下测试,有兴趣的载下来看看,自己做 ...
- webvtt字幕转srt字幕的python程序(附改名程序)
最近写了两个比较简单的python程序,原有都是由于看公开课感觉比较费劲,一个是下载的视频无用的名字太长,另一个就是下载的vtt字幕播放器不识别,写了一个vtt转换成str字幕格式的文件 vtt to ...
- python中函数基础
函数 什么是函数? 函数分为内置函数和自定义函数 定义:在程序中具备某一功能的工具.在使用之前需准备该工具(函数的定义),遇到应用场景拿来就用(后引用). 为什么要用函数? 1.代码冗余 程序组织结构 ...
- 4.ClassLink - 一种新型的VPC 经典网络的连接方式
阿里云CLassLink文档地址:https://help.aliyun.com/document_detail/65412.html?spm=a2c4g.11186623.2.9.41a25a07F ...
- VULKAN学习资料收集
https://github.com/vinjn/awesome-vulkan 张静初 https://zhuanlan.zhihu.com/p/24798656 知乎 https://develop ...
- 剑指offer例题——跳台阶、变态跳台阶
题目:一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 思路: n<=0时,有0种跳法 n=1时,只有一种跳法 n=2时,有 ...
- 使用xshell xftp连接centos的过程。
1 配置网络 登录Centtos系统,配置网卡. cd 空格/etc/sysconfig/network-scripts 回车键. ls 查看文件 vi ifcig-eth0 编辑这个文件 修改为 ...
- python 问答
1.list和tuple有什么区别? list是可变的,可以添加list.append,可以插入list.insert,可以改变元素值list[2] ='a':而tuple在初始化的时候就确定了,不能 ...
- [Redis]Redis的快速配置使用(图)
--------------------------------------------------------------------------------------------------- ...