[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 ...
随机推荐
- Oracle的操作系统身份认证(转)
oraclelogin数据库远程登录authenticationossqlnet.authentication_services=(NTS),在$ORACLE_HOME/network/admin/s ...
- MySQL创建函数报“ERROR 1418 ”错误,不能创建函数
MySQL创建函数报ERROR 1418错误,不能创建函数,根据官方提示是说,不能创建函数可能是一个安全设置方面的配置或功能未开启原因,下面我们一起来看. 错误 ERROR 1418 (HY000 ...
- android studio 3.0.1使用笔记(二)
发布前如何生成正式签名的APK? 一,查看APK签名方法:??Preferences->Android->Build可以查看到这个默认keystore文件的位置. 二,新建正式签名过程: ...
- 使用MVC实现登录功能
首先,从底层开始即Models: (1)通用数据访问类(封装数据访问类方法):SqlHelper类 使用命名空间:using System.Data; using System.Data.SqlCli ...
- 「小程序JAVA实战」小程序视频列表到详情功能(58)
转自:https://idig8.com/2018/09/23/xiaochengxujavashizhanxiaochengxushipinliebiaodaoxiangqinggongneng57 ...
- 人脸识别-<转>
人脸检测库libfacedetection介绍 libfacedetection是于仕琪老师放到GitHub上的二进制库,没有源码,它的License是MIT,可以商用.目前只提供了windows 3 ...
- python's default parameter
[python's default parameter] 对于值类型(int.double)的default函数参数,函数不会保存对默认类型的修改.对于mutable objectd类型的默认参数,会 ...
- java关于密码的加密解密
密码的加密方法有多种,常见的为Aes.Md5 Aes加密,可逆. 其中,Md5加密是采用了散列算法,也就是哈希算法,可以进行多次散列加密.Md5加密是不可逆的,无法解密. MD5是不可逆的单向加密方式 ...
- FluentValidation 模型验证
FluentValidation 是 .NET 下的模型验证组件,和 ASP.NET MVC 基于Attribute 声明式验证的不同处,其利用表达式语法链式编程,使得验证组件与实体分开.正如 Flu ...
- 基于python的Appium自动化测试的坑
真的感谢@虫师 这位来自互联网的老师,让我这个原本对代码胆怯且迷惑的人开始学习自动化测试. 一开始搜索自动化测试的时候,虫师的博客园教程都是在百度的前几位的,我就跟着虫师博客园里面的教程学习.后来学s ...