LeetCode之Symmetric Tree
</pre>Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).<p></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">For example, this binary tree is symmetric:</p><pre style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857143; color: rgb(51, 51, 51); word-break: break-all; word-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px;"> 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.
/**
* 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 left_right_Symmetric(TreeNode* pleft,TreeNode* pright)
{
if(pleft==NULL&&pright==NULL) return true;
if(pleft==NULL&&pright!=NULL)return false;
if(pleft!=NULL&&pright==NULL)return false;
if(pleft->val!=pright->val) return false;
return left_right_Symmetric(pleft->left,pright->right)&&left_right_Symmetric(pleft->right,pright->left);
}
public:
bool isSymmetric(TreeNode* root) {
if(root == NULL) return true;
if(root->left!=NULL&&root->right==NULL) return false;
if(root->left==NULL&&root->right!=NULL) return false;
if(root->left!=NULL&&root->right!=NULL&&root->left->val!=root->right->val)return false;
else return left_right_Symmetric(root->left,root->right);
}
};
LeetCode之Symmetric Tree的更多相关文章
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- [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 ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- (二叉树 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 题解]: Symmetric Tree
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ...
- leetcode 101 Symmetric Tree ----- java
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 ...
随机推荐
- eclipse导入svn中的web工程,部署到tomcat时候,只有WEB-INF目录问题
eclipse版本不同,上传工程svn中的settings文件,容易导致别的版本的eclipse,tomcat启动失败
- core2.1独立布署,报错的原因。
除了所有的.dll和exe 还有一个特别重要的文件.deps.json,
- 翻译:Laravel-4-Generators 使用自己定义代码生成工具高速进行Laravel开发
使用自己定义代码生成工具高速进行Laravel开发 这个Laravle包提供了一种代码生成器,使得你能够加速你的开发进程.这些生成器包含: generate:model – 模型生成器 generat ...
- librtmp将本地FLV文件发布到RTMP流媒体服务器
没有用到ffmpeg库 可以将本地FLV文件发布到RTMP流媒体服务器 使用librtmp发布RTMP流可以使用两种API:RTMP_SendPacket()和RTMP_Write(). 使用RTMP ...
- js学习笔记23----窗口尺寸及窗口事件
窗口尺寸: 可视区的尺寸 document.documentElement.clientWidth document.documentElement.clientHeight 滚动距离 documen ...
- C++之运算符重载
C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...
- ResNet 结构理解
博客来源于:https://blog.csdn.net/buyi_shizi/article/details/53336192:https://blog.csdn.net/dcrmg/article/ ...
- Linux命令之乐--nmap
Nmap是一款非常强大的实用工具,可用于:检测活在网络上的主机(主机发现)检测主机上开放的端口(端口发现或枚举)检测到相应的端口(服务发现)的软件和版本检测操作系统,硬件地址,以及软件版本检测脆弱性的 ...
- 《C++ Primer Plus》第5章 循环和关系表达式 学习笔记
C++提供了3种循环: for 循环. while 循环 和 do while 循环 .如果循环测试条件为 true 或非零,则循环将重复执行一组指令: 如果测试条件为 false 或 0 , 则结束 ...
- 报错:org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
org.springframework.http.converter.json.MappingJacksonHttpMessageConverter 1.错误描述 严重: Servlet /hux ...