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

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following [1,2,2,null,3,null,3] is not:

    1
/ \
2 2
\ \
3 3

Note:
Bonus points if you could solve it both recursively and iteratively.

/**
* 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 isSymmetric(TreeNode* root) {
if (!root) return true; TreeNode *left;
TreeNode *right; queue<TreeNode *> q1, q2;
q1.push(root->left); q2.push(root->right); while (!q1.empty() && !q2.empty())
{
left = q1.front(); q1.pop();
right = q2.front(); q2.pop(); if (NULL == left && NULL == right)
continue;
if (NULL == left || NULL == right)
return false;
if (left->val != right->val)
return false; q1.push(left->left);
q1.push(left->right);
q2.push(right->right);
q2.push(right->left);
} return true;
}
};

LeetCode 101. Symmetric Tree(镜像树)的更多相关文章

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

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

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

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

  3. Leetcode 101 Symmetric Tree 二叉树

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

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

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

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

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

  6. LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

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

  7. leetcode 101 Symmetric Tree ----- java

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

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

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

  9. Java for LeetCode 101 Symmetric Tree

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

随机推荐

  1. 《linux就该这么学》课堂笔记06 编写shell脚本、if、for

    1.1.脚本的两种方式 交互式(Interactive):用户每输入一条命令就立即执行. 批处理(Batch):由用户事先编写好一个完整的Shell脚本,Shell会一次性执行脚本中诸多的命令. 1. ...

  2. cobbler部署错误总结

    web 报错500 Internal Server Error解决方案 在安装使用Cobbler web界面的时候提示HTTP 500错误,也就是服务器内部错误,检查防火墙和selinux都是为关闭状 ...

  3. pace.js[转载]

    pace.js监控了什么: pace.js对于加载进度监控了什么呢?通过阅读源码,我们看到整体的进度有四个部分组成:document,elements,eventLag和ajax这四种监视器(Moni ...

  4. SpringBoot项目的测试类

    1. package soundsystem; import static org.junit.Assert.*; import org.junit.Test; import org.junit.ru ...

  5. Go语言 - 反射

    reflect包 在Go语言的反射机制中,任何接口值都由是一个具体类型和具体类型的值两部分组成的(我们在上一篇接口的博客中有介绍相关概念). 在Go语言中反射的相关功能由内置的reflect包提供,任 ...

  6. 什么叫pure function(纯函数)

    (来自:http://en.wikipedia.org/wiki/Pure_function) 在计算机编程中,假如满足下面这两个句子的约束,一个函数可能被描述为一个纯函数: 给出同样的参数值,该函数 ...

  7. mui.fire()用法,触发目标窗口的自定义事件

    mui.fire( 目标窗口的webview , '自定义事件名' ,{参数列表}:) 目标窗口监听这个自定义事件 window.addEventListener('自定义事件名',function( ...

  8. 比较两个CSV的方法的探索

    1.开始时,我用ultraEdit来简单比较,需要排序,而且比较的结果也有限 2.Excel也有这样的功能,可是好复杂 3.而后,发现dbeaver这个tools,利用JDBC Driver(csvj ...

  9. A1130 | 中缀表达式、查找根节点

    代码: #include <stdio.h> #include <memory.h> #include <math.h> #include <string&g ...

  10. Luogu P2447 [SDOI2010]外星千足虫 高斯消元

    链接 给出的条件是异或类型的方程,可以直接用bitset优化高斯消元. 至于求K,在高斯消元时记录用到的最大的方程的编号即可. 代码: // luogu-judger-enable-o2 #inclu ...