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. 201621123006 《Java程序设计》第8周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: 由源代码可 ...

  2. tornado多路由示例

    main.py代码: # encoding: utf-8 """ @version: ?? @author: andu99 @contact: andux@qq.com ...

  3. 手动整合实现SSH项目开发02

    在bean包下建立User类和User.hbm.xml文件,实现User类和数据库表User的映射关系,具体User类不多说,User.hbm.xml如下: <?xml version=&quo ...

  4. zoj 1828 Fibonacci Numbers

    A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the firs ...

  5. java spring boot 出现 java.lang.UnsatisfiedLinkError

    java.lang.UnsatisfiedLinkError: E:\ruanjian\jdk\bin\tcnative-1.dll: Can't load IA 32-bit .dll on a A ...

  6. 《DSP using MATLAB》示例 Example 9.8

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  7. MD文件

    了解一个项目,恐怕首先都是通过其Readme文件了解信息.如果你以为Readme文件都是随便写写的那你就错了.github,oschina git gitcafe的代码托管平台上的项目的Readme. ...

  8. 在Mac上安装anaconda,在命令行中输入conda,提示不是有效命令的解决办法

    原链接:https://stackoverflow.com/questions/18675907/how-to-run-conda

  9. pthread访问调用信号线程的掩码(pthread_sigmask )

    掩码: 信号掩码 在POSIX下,每个进程有一个信号掩码(signal mask).简单地说,信号掩码是一个"位图",其中每一位都对应着一种信号.如果位图中的某一位为1,就表示在执 ...

  10. 大数据之 ZooKeeper原理及其在Hadoop和HBase中的应用

    ZooKeeper是一个开源的分布式协调服务,由雅虎创建,是Google Chubby的开源实现.分布式应用程序可以基于ZooKeeper实现诸如数据发布/订阅.负载均衡.命名服务.分布式协调/通知. ...