<LeetCode OJ> 101. Symmetric Tree
Submissions: 273390 Difficulty: Easy
给定一颗二叉树,检查是否镜像对称(环绕中心对称)
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.
confused what "{1,#,2,3}" means? >
read more on how binary tree is serialized on OJ.
Subscribe to see which companies asked this question
分析(下面答案有极少的案例未通过。是错误答案!留作分析与纪念):
思路首先:
中序遍历二叉树。再推断遍历结果的对称性
以题目为样例:中序结果3241423。推断序列显然对称
以下那个不正确称的样例:23123,推断序列显然不正确称
/**
* 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:
vector<int> inorderTraversal(TreeNode* root) {
if(root){
inorderTraversal(root->left);
result.push_back(root->val);
inorderTraversal(root->right);
}
return result;
} bool isSymmetric(TreeNode* root) {
if(root==NULL)
return true;
inorderTraversal(root);//获取中序结果
for(int i=0;i<result.size()/2;i++)//推断序列对称否
if(result[i]!=result[result.size()-1-i])
return false;
return true;
}
private:
vector<int> result;
};
经过一段时间的分析才发现:
1),对于二叉树形状太极端情况是无法分辨的,
2),那种本来不是对称二叉树可是他的遍历序列因为数字太巧合却是对称的就不行了!
举例情况1:他的中序遍历为。121,显然不正确称
1
\
2
\
1
举例情况2:他的中序遍历为,2222。可是显然不正确称
2
/ \
2 2
\
2
错误答案截止
学习别人的答案:
递归,从根节点開始,推断左节点的左子树与右节点的右子树,左节点的右子树与右节点的左子树是否相等就可以!
/**
* 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 *root) {
if (!root) return true;
return helper(root->left, root->right);
}
//推断节点的左右子树是否对称
bool helper(TreeNode* leftnode, TreeNode* rightnode) {
if (!leftnode && !rightnode) //左右子树均为空
return true; if (!leftnode || !rightnode) //单子树
return false; if (leftnode->val != rightnode->val) //左右子树不相等
return false;
//左节点的左子树与右节点的右子树。左节点的右子树与右节点的左子树
return helper(leftnode->left,rightnode->right) && helper(leftnode->right, rightnode->left);
}
};
学习别人的迭代:
class Solution {
public:
bool isSymmetric(TreeNode* root) {
queue<TreeNode*> queue;
if(!root)
return true;
queue.push(root->left);
queue.push(root->right);
TreeNode *leftnode,*rightnode;
while(!queue.empty())
{
leftnode = queue.front();
queue.pop();
rightnode = queue.front();
queue.pop();
if (!leftnode && !rightnode) //左右子树均为空
continue;
if (!leftnode || !rightnode) //单子树
return false;
if(leftnode->val!=rightnode->val)//不相等
return false;
queue.push(leftnode->right);
queue.push(rightnode->left);
queue.push(leftnode->left);
queue.push(rightnode->right);
}
return true;
}
};
小结:
哎.....
注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50562771
原作者博客:http://blog.csdn.net/ebowtang
<LeetCode OJ> 101. Symmetric Tree的更多相关文章
- [LeetCode&Python] Problem 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【leetcode❤python】101. Symmetric Tree
#-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):# def __init_ ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 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 (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- LeetCode之“树”:Symmetric Tree && Same Tree
Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...
- LeetCode(1) Symmetric Tree
从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, sym ...
随机推荐
- 【转】spring 装配Bean中构造参数的注入
转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...
- selenium 找不到元素 (显式等待 和隐式等待的区别)
selenium自动化页面元素不存在异常发生的原因有一下几点: (1)页面加载时间过慢,需要查找的元素程序已经完成但是页面还未加载成功.此时可以加载页面等待时间. (2)查到的元素没有在当前的ifra ...
- c语言中Triplet是什么意思?
此词条多出现于三元组抽象数据类型的定义. 例如: 数据结构编程试验中,构造三元组类型. 1.三元组抽象数据类型的定义 ADT Triplet { 数据对象:D={e1, e2, e3| e1, e2, ...
- Vue CLI3 关闭热替换后出现的warning
用vue cli3做项目的时候如果开启了typescript的严格模式,在dev server热替换的时候往往就会打出一大堆warning,严重的影响了编译效率.官方并没有提供关闭warning的ap ...
- 【转-记】mysql总结
1 | 查询所有数据 select * from Info 查所有数据 select Code,Name from Info 查特定列 2 | 根据条件查 select * from Inf ...
- HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)
6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...
- .NET Core CLI
NET Core 命令 一. 帮助命令 dotnet help 使用情况: dotnet [sdk-options] [command] [command-options] [arguments] 执 ...
- 整数快速乘法/快速幂+矩阵快速幂+Strassen算法
快速幂算法可以说是ACM一类竞赛中必不可少,并且也是非常基础的一类算法,鉴于我一直学的比较零散,所以今天用这个帖子总结一下 快速乘法通常有两类应用:一.整数的运算,计算(a*b) mod c 二.矩 ...
- Hadoop-eclipse插件配置
1.准备jar包与安装eclipse. 2.将jar包拷贝到eclipse/plugin.
- 18、Django实战第18天:课程机构收藏功能
这里点击"收藏"也是ajax异步操作,我在operation.model.py中创建了一个用户收藏表,其中fav_id字段,如果我们收藏的是课程,那就是课程id,如果收藏的是课程机 ...