首页 技术 正文
技术 2022年11月15日
0 收藏 447 点赞 3,864 浏览 1775 个字

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/

https://leetcode.com/problems/symmetric-tree/discuss/33054/Recursive-and-non-recursive-solutions-in-Java

LeetCode All in One 题目讲解汇总(持续更新中…)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919