[LeetCode OJ] 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.
confused what "{1,#,2,3}" means?
方法一:递归实现
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
bool canBeSymmetric(TreeNode *p, TreeNode *q)
{
if(p==NULL && q==NULL)
return true;
if((p==NULL && q!=NULL) || (p!=NULL && q==NULL))
return false; if(p->val != q->val)
return false;
bool b1 = canBeSymmetric(p->left, q->right); //若二叉树p和二叉树q对称,则 p 的左子树和 q 的右子树对称
if(b1==false)
return false;
bool b2 = canBeSymmetric(p->right, q->left); //若二叉树p和二叉树q对称,则 p 的右子树和 q 的左子树对称 return b2; } class Solution {
public:
bool isSymmetric(TreeNode *root) { if(root==NULL)
return true; return canBeSymmetric(root->left, root->right);
}
};
方法二:迭代实现
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
vector<int> PreOrderTraverse(TreeNode *p) //按根左右的顺序遍历二叉树,迭代实现
{
vector<int> result;
stack<TreeNode*> st; while(p || !st.empty())
{
if(p)
{
result.push_back(p->val);
st.push(p);
p = p->left;
}
else
{
result.push_back(); //用0表示空树
p = st.top();
st.pop();
p = p->right;
}
}
result.push_back();
return result;
} vector<int> ReversePreOrderTraverse(TreeNode *p) //按根右左的顺序遍历二叉树,迭代实现
{
vector<int> result;
stack<TreeNode*> st; while(p || !st.empty())
{
if(p)
{
result.push_back(p->val);
st.push(p);
p = p->right;
}
else
{
result.push_back(); //用0表示空树
p = st.top();
st.pop();
p = p->left;
}
}
result.push_back();
return result;
} class Solution {
public:
bool isSymmetric(TreeNode *root) { if(root==NULL)
return true; vector<int> ivec1 = PreOrderTraverse(root->left);
vector<int> ivec2 = ReversePreOrderTraverse(root->right); return ivec1==ivec2;
}
};
注:
发现对于两棵完全相同的二叉树,用"#"(方法二中是用整数0,也可以用其他的代替)代替空树,他们的先序遍历序列是完全相同的,
反之也成立,即若两颗二叉树的先序遍历序列(用"#"代替空树)完全相同,那么这两颗二叉树也必然相同。
然而这一事实对于用中序遍历序列则不成立,也就是说若两颗二叉树的中序遍历序列(用"#"代替空树)完全相同,这两颗二叉树不一定相同。举例如下:
2
/ 的中序遍历序列(左根右)为: #3#2# 先序遍历序列(根左右)为: 23### 后序遍历序列(左右根)为: ##3#2
3
不同 相同 不同 不同
3
\ 的中序遍历序列(左根右)为: #3#2# 先序遍历序列(根左右)为: 3#2## 后序遍历序列(左右根)为: ###23
2 这两颗树不同,但他们对应的中序遍历序列却相同。
[LeetCode OJ] Symmetric Tree的更多相关文章
- LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)
思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...
- 【leetcode】Symmetric Tree
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的
题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- [leetcode] 10. Symmetric Tree
这次我觉得我的智商太低,想了很久才写出来.题目是让求镜像二叉树判断,题目如下: Given a binary tree, check whether it is a mirror of itself ...
- [LeetCode 题解]: Symmetric Tree
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- ♫【模式】自定义函数(self-defining function)
<JavaScript模式> /** * 如果创建了一个新函数并且将其分配给保存了另外函数的同一个变量,那么就以一个新函数覆盖旧函数. * 在某种程度上,回收旧函数指针以指向一个新函数.而 ...
- 【转】在ubuntu环境下搭建svn server遇到的一些问题
原文网址:http://www.cnblogs.com/pcchinadreamfly/archive/2012/11/24/2786046.html 前段时间在ubuntu 12.04lts上倒腾了 ...
- poj 2151
http://poj.org/problem?id=2151 Check ...
- Unity3d shader之卡通着色Toon Shading
卡通着色的目的是为了让被着色物体显得过渡的不那么好,明暗交界线很明显,等等卡通风格的一系列特征, 也叫Non-photorealisticrendering非真实渲染 重点要做到两点: 1. 描 ...
- HDOJ/HDU 1250 Hat's Fibonacci(大数~斐波拉契)
Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...
- 大牛博客!Spark / Hadoop / Kafka / HBase / Storm
在这里,非常感谢下面的著名大牛们,一路的帮助和学习,给予了我很大的动力! 有了Hadoop,再次有了Spark,一次又一次,一晚又一晚的努力相伴! HBase简介(很好的梳理资料) 1. 博客主页:h ...
- 如何在word2007中并排查看对比显示两个文档
使用word编辑或修改文件时,有时会需要对两个文档进行对比,此时就应该使用并排查看功能. 点击“视图”菜单中的“并排查看” 所打开的两个文档就会同时打开,并排显示 可点击“同步滚动”设置或取消同步滚动
- java websockect
https://github.com/TooTallNate/Java-WebSocket (websockect类库包) http://blog.openlg.net/index.php/archi ...
- Qt的皮肤设计(Style Sheet)
Qt的皮肤设计,也可以说是对Qt应用程序的界面美化,Qt使用了一种类CSS的样式规则QSS. 一.Style Sheet的应用 1.直接在程序代码中设置样式,利用setStyleSheet()方法 ...
- Objective-C通过联合存储为类增加属性及原理解析
联合存储实现方式及底层原理解析 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory_tjsd/article/details/9347981 转载请注明出处 ...