<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 ...
随机推荐
- Javacore分析(转载)
本文转自(http://www.ibm.com/developerworks/cn/websphere/library/techarticles/1406_tuzy_javacore/1406_tuz ...
- Android开发工具--AndroidStudio
1.Android studio更改快捷键File->setttings 搜索key map就可以更改成自己喜欢的会计键风格了
- [ Openstack ] Openstack-Mitaka 高可用之 Dashboard
目录 Openstack-Mitaka 高可用之 概述 Openstack-Mitaka 高可用之 环境初始化 Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...
- 字节、字、bit、byte的关系[转]
字 word 字节 byte 位 bit 字长是指字的长度 1字=2字节(1 word = 2 byte) 1字节=8位(1 byte = 8bit) 一个字的字长为16 一个字节的字长是8 bps ...
- import module与from module import * 两种模块导入有何区别
#原创,转载请留言联系 区别1 import module 引用共享变量时,要使用module.变量名. 而from module import * 直接使用变量名即可 区别2(有点绕) import ...
- Selenium2+python自动化52-unittest执行顺序【转载】
前言 很多初学者在使用unittest框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行. 本篇通过最简单案例详细讲解unittest执行顺 ...
- Laravel5.5 生成测试数据
1.在database/factories/UserFactory.php 中添加 2.在tinker中生成数据 3.数据生成成功
- [BZOJ2006] [NOI2010]超级钢琴 主席树+贪心+优先队列
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 3591 Solved: 1780[Submit][Statu ...
- 前端读者 | 分分钟让你理解HTTPS
本文来自@Keely袁庆玲:来源:https://juejin.im/post/5ad6ad575188255c272273c4 目前来看大多数网站都从HTTP转向HTTPS,不在支持HTTP,所以了 ...
- Xamarin XAML语言教程构建ControlTemplate控件模板 (二)
Xamarin XAML语言教程构建ControlTemplate控件模板 (二) (2)打开MainPage.xaml文件,编写代码,将构建的控件模板应用于ContentView中.代码如下: &l ...