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的更多相关文章

  1. LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)

      思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...

  2. 【leetcode】Symmetric Tree

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  3. 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...

  4. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  5. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  6. [leetcode] 10. Symmetric Tree

    这次我觉得我的智商太低,想了很久才写出来.题目是让求镜像二叉树判断,题目如下: Given a binary tree, check whether it is a mirror of itself ...

  7. [LeetCode 题解]: Symmetric Tree

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述   Given a ...

  8. LeetCode 101. Symmetric Tree (对称树)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  9. (二叉树 DFS 递归) leetcode 101. Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

随机推荐

  1. Light OJ 1036 - A Refining Company

    题目大意: 一个m*n的矩阵,里面有两种矿物质铀和镭,现在要把铀和镭运送到指定位置.北边是炼镭厂,西边是了炼铀厂. 现在要建立传送带,传送带有两种,一种是从东到西,另一种是从南到北,传送带不能交叉,并 ...

  2. (转载)获取当前运行的PHP版本信息

    (转载)http://www.clovery.org/get-the-php-version-information.html 获取PHP运行环境信息,可以使用下面的函数. <?php phpi ...

  3. linux安装JDK TOMCAT

    1.下载包 到http://apr.apache.org/下载下面3个包 apr-1.4.2.tar.gz apr-iconv-1.2.1.tar.gz apr-util-1.3.10.tar.gz  ...

  4. 【转】 log4cpp 的使用

    [转自] http://sogo6.iteye.com/blog/1154315     Log4cpp配置文件格式说明   log4cpp有3个主要的组件:categories(类别).append ...

  5. Poj 3580-SuperMemo Splay

    题目:http://poj.org/problem?id=3580   SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submis ...

  6. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  7. 开源跨平台的3D渲染软件

    http://www.blender.org/ KVM是Kernel-based Virtual Machine的缩写; http://kiwik.github.io/openstack/2013/1 ...

  8. redis基本用法

    java连接redis基本用法 package Redis;    import java.util.HashMap;  import java.util.List;  import java.uti ...

  9. 小试牛刀——python接口测试小框架

    用例设计: 执行用例代码: # -*- coding: UTF-8 -*-import xlrd,logging,urllib,urllib2,json,sysfrom pylsy import py ...

  10. [React] React Fundamentals: with-addons - ReactLink

    It can be tedious to type out all the boilerplate needed to get the DOM and states in React to synch ...