Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy
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.
分析:
判断一棵树是否是镜像二叉树。首先,我对树的题目是心存畏惧的,因为这种题目往往涉及递归、循环,而且无法立刻给出思路。但是,现在想想,怕啥,因为关于树的题目套路比较多,通过见多识广,早晚有一天会“见怪不怪”。
首先,我们要明白什么是镜像二叉树——以中央作为对折点,左右两边可以重合,就像镜子一样。镜像二叉树有什么特点呢——根结点的左子树和右子树对称,即左子树的左节点对应着右子树的右节点,二者要相等,以此类推。
在写程序的时候就不难想出以下方法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root == nullptr)
return true;
return isSymmetricCore(root->left, root->right);
}
bool isSymmetricCore(TreeNode* leftRoot, TreeNode* rightRoot) {
if (leftRoot == nullptr && rightRoot == nullptr)
return true;
else if (leftRoot == nullptr || rightRoot == nullptr)
return false;
if (leftRoot->val == rightRoot->val)
return isSymmetricCore(leftRoot->left, rightRoot->right) && isSymmetricCore(leftRoot->right, rightRoot->left);
return false;
}
};
Leetcode之101. Symmetric Tree Easy的更多相关文章
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- 【LeetCode】101. Symmetric Tree (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- Leetcode 101. Symmetric Tree(easy)
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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】101 - Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode OJ 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 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- <LeetCode OJ> 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
随机推荐
- 05 django组件:contenttype
1.django组件:contenttype 组件的作用:可以通过两个字段让表和N张表创建FK关系 1.专题课,学位课 如何关联 过期时间?? 方法1:分别创建 专题课--过期时间表 .学位课--过期 ...
- Android控件_RecycleView+CarView+Palette联合应用
最终效果 表格布局 垂直布局 横向布局 添加引用 build.gradle implementation 'com.android.support:recyclerview-v7:28.0.0' im ...
- 给程序添加git commit信息
遇到了一个客户程序出问题,自己这边始终无法重现的bug.为了检查问题,查到了一个添加git的commit信息到程序中的方法,感觉对程序版本控制十分好用. 一,项目中添加如下文件 文件结构: GitVe ...
- Python序列构成的数组
1.内置序列类型 容器序列:list,tuple,collections.deque (能存放不同类型) 扁平序列:str,bytes,bytearray,memoryview,array.array ...
- Homebrew是什么?怎么关闭自动更新?
Homebrew是MacOS 的软件包管理器. 通过它可以安装.卸载.更新.查看.搜索任何想要安装的软件.如:git, node等. 安装Homebrew /usr/bin/ruby -e " ...
- Microsoft.Practices.Unity使用配置文件总是报错The type name or alias could not be resolved.
Type name could not be resolved. Please check config file http://stackoverflow.com/questions/1493564 ...
- 洛谷 P4933 大师
题面 (实名推荐:本题的出题人小哥哥打球暴帅哦!(APIO/CTSC/WC的时候一起打过球w,而且大学在我隔壁喔) ) 没仔细看数据范围的时候真是摸不着头脑...还以为要 O(N^2) dp 爆锤.. ...
- socketserver(多连接)
正如前面的socket模块部分看到的一样,写一个简单套接字服务器不是很难,如果想实现超出继承的应用,最好寻求一些帮助,socketserver模块是标准库中很多服务器框架的基础,这些服务器架构包括Ba ...
- 【Spring Boot】 Spring Boot 2.x 版本 CacheManager 配置方式
Spring Boot 1.X RedisCacheManager 配置方式 @Bean public CacheManager cacheManager(RedisTemplate redisTem ...
- IntelliJ IDEA 2017.3 搭建一个多模块的springboot项目(二)
上一篇我成功搭建了一个项目,名叫bale-project,下面我们继续搭建子模块. 在项目名称上右键,New->Module,新建一个模块. 这次我们选择Spring Initializr 起个 ...