[LeetCode 题解]: Symmetric Tree
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
2. 题意
给定一颗二叉树,判断该树是否为左右对称的二叉树。
3. 思路
4: 解法
class Solution {
public:
bool isSymmetric(TreeNode *root){
return root? Symmetric(root->left,root->right):true;
}
bool Symmetric(TreeNode *left, TreeNode *right){
if(left==NULL && right==NULL) return true;// 左右孩子为空
if(!left || !right) return false; // 仅含有左子树或者右子树
return left->val == right->val
&& Symmetric(left->left,right->right)
&& Symmetric(left->right,right->left);
}
};![]() |
作者:Double_Win 出处: http://www.cnblogs.com/double-win/p/3891215.html 声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~ |
[LeetCode 题解]: Symmetric Tree的更多相关文章
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- [LeetCode 题解]: Binary Tree Preorder Traversal
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 【leetcode】Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 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] 10. Symmetric Tree
这次我觉得我的智商太低,想了很久才写出来.题目是让求镜像二叉树判断,题目如下: Given a binary tree, check whether it is a mirror of itself ...
- 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
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- 基于nginx-rtmp-module模块实现的HTTP-FLV直播模块(nginx-http-flv-module)
本文后续的内容将在这里更新:<基于nginx-rtmp-module模块实现的HTTP-FLV直播模块(nginx-http-flv-module)续>.注意:下文的配置很多已经不能用了, ...
- bzoj 3159: 决战
Description 树上链翻转,链加,查询链上的和/max/min 树链剖分套treap,修改查询可以用类似线段树的写法,翻转可以利用分裂合并和翻转标记实现,时间复杂度O(nlog2n) 实测除了 ...
- UI“三重天”之selenium--常用API和问题处理(三)
Selenium常用API: 前面两篇示例代码中用到了一些selenium的API方法,例如定位元素的八种方法.访问url.等待.操作浏览器.获取title.点击.清理等等. 有关于selenium的 ...
- PHP session 跨子域问题总结 ini_set('session.cookie_domain', ".domain.com")
Session主要分两部分: 一个是Session数据,该数据默认情况下是存放在服务器的tmp文件下的,是以文件形式存在 另一个是标志着Session数据的Session Id,Session ID, ...
- FancyButtons一个漂亮按钮的库
一个功能强大且全面的按钮控件,是目前我见过的最好使的按钮. 支持给按钮添加图标,并且可通过属性设置手指按钮的效果,不需要在写<selector>文件. 项目地址:https://git ...
- curl获取响应时间
1.开启gzip请求curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte 2.监控网页的响应时间curl -o /dev/nu ...
- Vim插件之ale,LeaderF,completor.vim(win10)配置
内容包含 vim-plug,异步插件管理,总之就是下起来快. ale,异步语法检查 LeaderF,快速查找文件 completor.vim vim8的快速补全 markdown预览 common s ...
- TDataset.CopyFields
Description Often when manipulating datasets with similar structures, you need to copy the records f ...
- FORALL用法小结
本文主要翻译.整理了ORACLE官方文档上有关FORALL的部份内容,不妥之处,还希望多和大家交流. 在发送语句到SQL引擎前,FORALL语句告知PL/SQL 引擎批挷定输入集合.尽管FORALL语 ...
- 安装nvidia driver
ubuntu16.04 下载地址 http://www.nvidia.com/Download/index.aspx dpkg -i nvidia-diag-driver-local-repo-ubu ...
