LeetCode 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
/**
* 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 *lhs, TreeNode *rhs)
{
if (NULL == lhs&&NULL == rhs)return true;
if (NULL!=lhs&&NULL!=rhs&&lhs->val == rhs->val)
{
return isSymmetric(lhs->right, rhs->left) && isSymmetric(lhs->left, rhs->right);
}
return false;
}
bool isSymmetric(TreeNode* root) {
if (!root)return true;
return isSymmetric(root->left, root->right);
}
};
LeetCode 101. Symmetric Tree的更多相关文章
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 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 101 Symmetric Tree ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Java [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 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 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对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- 【转】PHP中require和include路径问题总结
1 绝对路径.相对路径和未确定路径 2. 相对路径: 3. 绝对路径 4. 未确定路径 5. 解决方案 1 绝对路径.相对路径和未确定路径 相对路径 相对路径指以.开头的路径,例如 ./a/a.php ...
- Externalizable接口 序列化
Java默认的序列化机制非常简单,而且序列化后的对象不需要再次调用构造器重新生成,但是在实际中,我们可以会希望对象的某一部分不需要被序列化,或者说一个对象被还原之后, 其内部的某些子对象需要重新创建, ...
- 计时器Chronometer和时钟(AnalogClock和DigitalClock)
计时器Chronometer和时钟(AnalogClock和DigitalClock) (1)Android提供了两个时钟组件:AnalogClock和DigitalClock,DigitalCloc ...
- 微信支付(0923更新)商户支付密钥key的生成与设置
微信支付(0923更新)商户支付密钥key的生成与设置 说明:新版微信支付,用户必须授权登录才能支付.需要商家自己设置商户号支付密钥. 设置商户号支付密钥方法如下: 1. 申请通过审核后,打开微信发来 ...
- mybatis 中的稍微复杂些的sql语句
mybatis 中的稍微复杂些的sql语句: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP ...
- 51nod1253 Kundu and Tree
树包含N个点和N-1条边.树的边有2中颜色红色('r')和黑色('b').给出这N-1条边的颜色,求有多少节点的三元组(a,b,c)满足:节点a到节点b.节点b到节点c.节点c到节点a的路径上,每条路 ...
- Winfrom DateGridView 实现Button列禁用
Form窗体如下所示: 实现如下: using System; using System.Collections.Generic; using System.Drawing; using System ...
- Winform/WPF国际化处理
1.Winfrom国际化处理方式 ==> Winform中国际化支持可将UI页面和.cs文件分开处理 处理窗体方式如下:1.选择Form窗体设置其--Language(默认中文--Default ...
- Thinkpad X240修改bios引导方式
来源:http://blog.csdn.net/jsship/article/details/19121149 修改笔记本的BIOS设置!这是非常重要的步骤之一.否则,你的U盘不能引导手提电脑进入PE ...
- Navicat(连接) -1之Navicat Cloud
Navicat Cloud Navicat Cloud 提供一个云服务,同步不同机器和平台的 Navicat 连接.查询.模型和虚拟组.当添加一个连接到 Navicat Cloud,它的连接设置和查询 ...