[LeetCode] Symmetric Tree 判断对称树
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.
判断二叉树是否是平衡树,比如有两个节点n1, n2,我们需要比较n1的左子节点的值和n2的右子节点的值是否相等,同时还要比较n1的右子节点的值和n2的左子结点的值是否相等,以此类推比较完所有的左右两个节点。我们可以用递归和迭代两种方法来实现,写法不同,但是算法核心都一样。
解法一:
class Solution {
public:
bool isSymmetric(TreeNode *root) {
if (!root) return true;
return isSymmetric(root->left, root->right);
}
bool isSymmetric(TreeNode *left, TreeNode *right) {
if (!left && !right) return true;
if (left && !right || !left && right || left->val != right->val) return false;
return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left);
}
};
迭代写法需要借助两个队列queue来实现,我们首先判空,如果root为空,直接返回true。否则将root的左右两个子结点分别装入两个队列,然后开始循环,循环条件是两个队列都不为空。在while循环中,我们首先分别将两个队列中的队首元素取出来,如果两个都是空结点,那么直接跳过,因为我们还没有比较完,有可能某个结点没有左子结点,但是右子结点仍然存在,所以这里只能continue。然后再看,如果有一个为空,另一个不为空,那么此时对称性已经被破坏了,不用再比下去了,直接返回false。若两个结点都存在,但是其结点值不同,这也破坏了对称性,返回false。否则的话将node1的左子结点和右子结点排入队列1,注意这里要将node2的右子结点和左子结点排入队列2,注意顺序的对应问题。最后循环结束后直接返回true,这里不必再去check两个队列是否同时为空,因为循环结束后只可能是两个队列均为空的情况,其他情况比如一空一不空的直接在循环内部就返回false了,参见代码如下:
解法二:
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (!root) return true;
queue<TreeNode*> q1, q2;
q1.push(root->left);
q2.push(root->right);
while (!q1.empty() && !q2.empty()) {
TreeNode *node1 = q1.front(); q1.pop();
TreeNode *node2 = q2.front(); q2.pop();
if (!node1 && !node2) continue;
if((node1 && !node2) || (!node1 && node2)) return false;
if (node1->val != node2->val) return false;
q1.push(node1->left);
q1.push(node1->right);
q2.push(node2->right);
q2.push(node2->left);
}
return true;
}
};
参考资料:
https://leetcode.com/problems/symmetric-tree/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Symmetric Tree 判断对称树的更多相关文章
- 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 (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [LeetCode] Same Tree 判断相同树
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- Symmetric Tree,对称树
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- 【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】
[101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, check whether ...
- [Leetcode] Same tree判断是否为相同树
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- LeetCode: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)
思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...
随机推荐
- 用 jQuery.ajaxSetup 实现对请求和响应数据的过滤
不知道同学们在做项目的过程中有没有相同的经历呢?在使用 ajax 的时候,需要对请求参数和响应数据进行过滤处理,比如你们觉得就让请求参数和响应信息就这么赤裸裸的在互联网里来回的穿梭,比如这样: 要知道 ...
- 2.C#面向对象基础属性
属性: 1.一般属性开头字母大写,字段开头字母小写. 2.通过public来赋值的方法无法判断赋值是否非法! 3.无论赋值如何,取值如果就是不采用赋值的结果,那么无论赋值什么都不管用. 4.经典错误之 ...
- mysql+mycat搭建稳定高可用集群,负载均衡,主备复制,读写分离
数据库性能优化普遍采用集群方式,oracle集群软硬件投入昂贵,今天花了一天时间搭建基于mysql的集群环境. 主要思路 简单说,实现mysql主备复制-->利用mycat实现负载均衡. 比较了 ...
- conversations.im
最后在即时消息 https://conversations.im/ https://github.com/chrisballinger/ 对话是Android 4.0的Jabber / XMPP客户 ...
- Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App
安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...
- 现有语言不支持XXX方法
史上最强大的IDE也会有bug的时候哈,今天遇到这个问题特别郁闷,百度了下,果然也有人遇到过这个问题 解决方法: 1.调用的时候参数和接口声明的参数不一致(检查修改) 2.继承接口中残留一个废弃的方法 ...
- C# - Networkcomms
来自英国的用C#语言编写的开源的TCP/UDP网络通信框架,简单方便,性能稳定. 参考: NetworkComms官网: NetworkComms通信框架中文网: 介绍开源的.net通信框架: Ne ...
- angular的$filter服务
首先,介绍下$filter服务: 1.$filter是用来进行数据格式化的专用服务: 2.AngularJS内置了currency.date.filter.json.limitTo.lowercase ...
- ios新手开发——toast提示和旋转图片加载框
不知不觉自学ios已经四个月了,从OC语法到app开发,过程虽然枯燥无味,但是结果还是挺有成就感的,在此分享我的ios开发之路中的小小心得~废话不多说,先上我们今天要实现的效果图: 有过一点做APP经 ...
- 5.6 JS中基本包装类型
为了便于操作基本类型值,ES还提供了三种特殊的引用类型,即(基本包装类型):Number,String,Boolean.这三种类型与前面介绍的引用类型相似,但同时也拥有基本数据类型的一些特性. 平时经 ...