【LeetCode】Symmetric Tree(对称二叉树)
这道题是LeetCode里的第101道题。是我在学数据结构——二叉树的时候碰见的题。
题目如下:
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/ \
2 2
\ \
3 3
说明:
你可以运用递归和迭代两种方法解决这个问题。
解题思路:
读完题目后我首先想到的是用遍历的方法来解题,树的遍历有三种:前序遍历,中序遍历和后序遍历。
这里我选择的是中序遍历,对根节点的左右子树分别进行中序遍历,但是因为要符合题目的要求——对称,所以两种中序遍历的方式不一样,根节点左子树采用LDR中序遍历,根节点右子树采用RDL中序遍历。
如同上面的两个示例:第一颗树根节点的左子树遍历结果是[3,2,4],而根节点的右子树遍历结果是[3,2,4],相等。对称。
第二颗树根节点的左子树遍历结果是[2,3],而根节点的右子树遍历结果是[3,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:
vector<int> LPreOrder(TreeNode* tree){//LDR中序遍历
stack<TreeNode*>st;//节点堆栈
vector<int>res;
TreeNode* pt;
if(tree==NULL)return res;
pt=tree;
while(pt!=NULL||st.size()!=){
while(pt!=NULL){//先访问左节点
st.push(pt);
pt=pt->left;
}
pt=st.top();//最下边的左节点
res.push_back(pt->val);//保存数值
st.pop();
pt=pt->right;//访问右节点
}
return res;
}
vector<int> RPreOrder(TreeNode* tree){//RDL中序遍历
stack<TreeNode*>st;
vector<int>res;
TreeNode* pt;
if(tree==NULL)return res;
pt=tree;
while(pt!=NULL||st.size()!=){
while(pt!=NULL){
st.push(pt);
pt=pt->right;
}
pt=st.top();
res.push_back(pt->val);
st.pop();
pt=pt->left;
}
return res;
}
bool isSymmetric(TreeNode* root) {
if(root==NULL)//根节点是否为空
return true;
if(root->left==NULL||root->right==NULL){//左右子树其中一个是否为空
if(root->left==NULL&&root->right==NULL)//左右子树是否为空
return true;
return false;
}
else{
if(root->left->val!=root->right->val)return false;
vector<int>Lorder,Rorder;
Lorder=LPreOrder(root->left);//遍历
Rorder=RPreOrder(root->right);//遍历
if(Lorder!=Rorder)return false;//判断遍历结果是否相同
return true;
}
}
};
运行结果:

测试结果通过。。。
这其实这还没结束,因为如果是这颗树的话(自己突然想到的):

很明显不对称是不是,但是仍然能通过测试,就说明在测试案例里没有这颗树。钻了个空子。
然后我没办法只好用官方解题思路:
就是使用双节点递归对每一个节点及其相对称的节点进行左右对称判断。类似于广度优先算法,或是层次遍历。
/**
* 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 isMirror(TreeNode* t1,TreeNode* t2){
if(t1==NULL&&t2==NULL)return true;
if(t1==NULL||t2==NULL)return false;
return (t1->val==t2->val)
&&isMirror(t1->left,t2->right)
&&isMirror(t2->left,t1->right);
}
bool isSymmetric(TreeNode* root) {
return isMirror(root,root);
}
};
是不是看起了简单多了?我也学习到了。
个人总结:写算法不能只光顾一个案例,要善于使用各种特殊的案例来测试算法的正确性和可行性,在这里我就是在编写过程中没有充分考虑到空节点的情况,导致了多次提交的错误。
【LeetCode】Symmetric Tree(对称二叉树)的更多相关文章
- [Leetcode] 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 & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 二叉树系列 - [LeetCode] 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 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- LeetCode: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- LeetCode【101. 对称二叉树】
对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...
- [LeetCode] 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对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 第28题:leetcode101:Symmetric Tree对称的二叉树
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...
随机推荐
- 【bzoj1718】Redundant Paths 分离的路径
1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 964 Solve ...
- Codeforces Round #390 (Div. 2) A
One spring day on his way to university Lesha found an array A. Lesha likes to split arrays into sev ...
- YDKJ 读书笔记 01 Function vs. Block Scope
Introduction 本系列文章为You Don't Know JS的读书笔记. 书籍地址:https://github.com/getify/You-Dont-Know-JS Scope Fro ...
- 使用express4.x版、Jade模板以及mysql重写《nodejs开发指南》微博实例
最近阅读<nodejs开发指南>一书,书是不错的,然而其微博代码示例用的是express3.x,用些过时了,运行代码出现不少bug(我电脑安的是express4.x),于是用express ...
- P1216 [USACO1.5]数字三角形 Number Triangles
题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 ...
- mui开发中获取单选按钮、复选框的值
js获取单选按钮的值 function getVals(){ var res = getRadioRes('rds'); if(res == null){mui.toast('请选择'); retur ...
- setuid
-r-s--x--x #s就是setuid,仅可用在二进制文件,对目录设置无效
- Java文件操作系列[3]——使用jacob操作word文档
Java对word文档的操作需要通过第三方组件实现,例如jacob.iText.POI和java2word等.jacob组件的功能最强大,可以操作word,Excel等格式的文件.该组件调用的的是操作 ...
- C#遍历文件夹下全部文件
public static List<string> GetFile(string path, List<string> FileList, string RelativePa ...
- (十二)mybatis之动态代理
mybatis之动态代理的应用 在前文(https://www.cnblogs.com/NYfor2018/p/9093472.html)我们知道了,Mybatis的使用需要用到Mapper映射文件, ...