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.
public bool IsSymmetric(TreeNode root)
{
return IsMirror(root?.left, root?.right);
} public bool IsMirror(TreeNode left, TreeNode right)
{
bool flag;
if (left == null && right == null)
{
flag = true;
}
else if (left == null || right == null)
{
flag = false;
}
else
{
if (left.val == right.val)
{
flag = IsMirror(left.left, right.right) && IsMirror(left.right, right.left);
}
else
{
flag = false;
}
} return flag;
}
101. Symmetric对称 Tree的更多相关文章
- [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 ...
- Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- 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 判断对称树 C++
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). ...
- [leetcode]101. Symmetric Tree对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- [ Learning ] Design Pattens
1. 单例2. 模板3. 代理,装饰 (代理和装饰的区别)4. 状态
- 31网络通信之Select模型
多路复用并发模型 -- select #include<sys/select.h> #include<sys/time.h> int select(int maxfd, f ...
- 设计模式之Template(模板)(转)
Template定义: 定义一个操作中算法的骨架,将一些步骤的执行延迟到其子类中. 其实Java的抽象类本来就是Template模式,因此使用很普遍.而且很容易理解和使用,我们直接以示例开始: pub ...
- c# 规范用户输入控件
MaskedTextBox控件是一种特殊的文本框,它可以通过Mask属性设置格式标记符.在应用程序运行后,用户只能输入Mask属性允许的内容,列入日期.电话等 在“输入掩码”对话框的右下角有一个“使用 ...
- Hinton“深度学习之父”和“神经网络先驱”,新论文Capsule将推翻自己积累了30年的学术成果时
Hinton“深度学习之父”和“神经网络先驱”,新论文Capsule将推翻自己积累了30年的学术成果时 在论文中,Capsule被Hinton大神定义为这样一组神经元:其活动向量所表示的是特定实体类型 ...
- python 图片
- hdu1599 find the mincost route
题目链接 floyd找最小环 很好理解 #include<algorithm> #include<iostream> #include<cstdlib> #incl ...
- [转载] 关于出现“使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式”错误的可能原因
1. 对于该问题确实存在UNION前后SELECT语句中的列数不同导致:2. 以下为个人遇到的一种可能:在项目开发中由于有张表是动态的,即有个基础表,其他的表按年月根据基础表来生成动态表,动态表结构和 ...
- flask框架----flask基础
知识点回顾 1.flask依赖wsgi,实现wsgi的模块:wsgiref,werkzeug,uwsgi 2.实例化Flask对象,里面是有参数的 app = Flask(__name__,templ ...
- iOS项目之“返回”手势操作相关
在程序中,总会设置“返回”按钮,但不可能在每一个控制器中都去设置一次“返回”按钮,那如何设置全局的“返回”按钮呢? 首先自定义一个导航控制器,在tabBarController中添加子控制器时,使用这 ...