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.

思路:要等到左儿子和右儿子的结果都知道了,才能判断当前节点的对称性,所以是后序遍历

法I:递归后序遍历

class Solution {
public:
bool isSymmetric(TreeNode *root) {
if(!root) return true; bool result;
if((root->left==NULL && root->right != NULL) ||(root->left!=NULL && root->right == NULL))
{
return false;
}
else if(root->left == NULL && root->right == NULL)
{
return true;
}
else
{
result = cmp(root->left, root->right);
}
}
bool cmp(TreeNode * node1, TreeNode* node2)
{
int result1 = true;
int result2 = true;
if(node1->val!=node2->val) return false;
else
{
//递归结束条件:至少有一个节点为NULL
if((node1->left==NULL && node2->right != NULL) ||
(node1->left!=NULL && node2->right == NULL)||
(node1->right!=NULL && node2->left == NULL)||
(node1->right==NULL && node2->left != NULL))
{
return false;
}
if((node1->left == NULL && node2->right == NULL)&&
(node1->right == NULL && node2->left== NULL))
{
return true;
} //互相比较的两个点,要比较节点1的左儿子和节点2的右儿子,以及节点1的右儿子和节点2的左儿子
if(node1->left != NULL)
{
result1 = cmp(node1->left,node2->right);
}
if(node1->right != NULL)
{
result2 = cmp(node1->right,node2->left);
}
return (result1 && result2);
}
}
};

法II:用队列实现层次遍历(层次遍历总是用队列来实现)

class Solution {
public:
bool isSymmetric(TreeNode *root) {
if(root == NULL) return true;
queue<TreeNode*> q;
q.push(root->left);
q.push(root->right);
TreeNode *t1, *t2;
while(!q.empty()){
t1 = q.front();
q.pop();
t2 = q.front();
q.pop();
if(t1 == NULL && t2 == NULL)
continue;
if(t1 == NULL || t2 == NULL || t1->val != t2->val)
return false;
q.push(t1->left);
q.push(t2->right);
q.push(t1->right);
q.push(t2->left);
}
return true;
}
};

101. Symmetric Tree (Tree, Queue; DFS, WFS)的更多相关文章

  1. 101. Symmetric对称 Tree

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

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

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

  3. &lt;LeetCode OJ&gt; 101. Symmetric Tree

    101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...

  4. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  5. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

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

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

  7. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  8. LeetCode 101. Symmetric Tree 判断对称树 C++

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

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

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

随机推荐

  1. 集成学习之Boosting —— AdaBoost实现

    集成学习之Boosting -- AdaBoost原理 集成学习之Boosting -- AdaBoost实现 AdaBoost的一般算法流程 输入: 训练数据集 \(T = \left \{(x_1 ...

  2. [置顶] Android 打包apk无敌报错

    前言: 这个问题从昨天上午一直到现在,请教了很多大佬,都没有给出确定的解决方案,可能他们也没碰到过可能,不过还是挺感谢他们的建议,一直到今天中午午休,我一直都在想这个问题,就是下面的这个,看了国内很多 ...

  3. HTML页面每次打开的时候都清除页面缓存

    解决办法为: (1) 用HTML标签设置HTTP头信息 <HEAD> <META    HTTP-EQUIV="Pragma" CONTENT="no- ...

  4. 第24课 #pragma使用分析

    #pragma是C语言留给编译器厂商进行扩展用的. 这个关键字在不同的编译器之间也许是不能够移植的. #pragma简介 #pragma message #pragma message打印的消息并不代 ...

  5. Qt 编译完后指定输出路径

    make install INSTALL_ROOT=/home/hotot/qt4rls

  6. [BZOJ3470]Freda’s Walk

    bzoj description 雨后的Poetic Island空气格外清新,于是Freda和Rainbow出来散步. Poetic Island的交通可以看作一张\(n\)个点.\(m\)边的有向 ...

  7. 【转】Python 中的 if __name__ == '__main__' 该如何理解

    转自:http://blog.konghy.cn/2017/04/24/python-entry-program/ 程序入口 对于很多编程语言来说,程序都必须要有一个入口,比如 C,C++,以及完全面 ...

  8. centos7 安装配置rsyslog + LogAnalyzer + mysql

    https://www.cnblogs.com/mchina/p/linux-centos-rsyslog-loganalyzer-mysql-log-server.html 安装LNMP 一键安装包 ...

  9. SpringCloud初体验:一、Eureka 服务的注册与发现

    Eureka :云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. Eureka 可以大致理解为 房产中介 和 房东 的关系,房东想让租客租房子,首先要把房子 ...

  10. mysqlinsert触发器的创建

    CREATE DEFINER=`wpsuper`@`%` TRIGGER `o2oinsert` BEFORE INSERT ON `t_s_o2o`FOR EACH ROW begin set Ne ...