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. 初步学习JAVA面向对象初步认识及面向对象内存分析图举例说明

    1. 说到面向对象, 一个绕不开的话题,就是面向过程. 面向过程适合简单.不需要协作的事务. 面向过程 = 分解问题 + 逻辑为先 = 先细节,再整体. 对比面向过程, 面向对象是模块化的, 当我们思 ...

  2. docker学习9-搭建rabbitMQ环境

    前言 docker搭建rabbitMQ环境 下载镜像 rabbitMQ 镜像仓库地址https://hub.docker.com/_/rabbitmq 找带有 mangement的版本,会带后台管理界 ...

  3. sublime设置代码缩进

    打开sublime的首选项(Preferences)下的设置-用户(Setting-User) ,配置如下代码 , "translate_tabs_to_spaces": true ...

  4. 手抄吧1:windows web server

    字母写的他妈的 太恶心了  以后努力改  天天敲代码  好恶心的字体

  5. 20180523模拟赛T1——前缀?

    (a.cpp/c/pas) Time Limit:1 Sec Memory Limit:128 MB 简化版题意 jyt毒瘤,写了超长的题面,要看完整题面的翻到最后-- 老太太认为一个长度为 N 的仅 ...

  6. Excel——读取——导出目录

    /** * 导出Excel文件到具体的目录 * <一句话功能简述> * <功能详细描述> * @param fileName 导出的文件名 * @param sheetName ...

  7. Go语言 - 反射

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

  8. VSCode 如何操作用户自定义代码片段

    自己写了一些根据自己习惯弄成的自定义代码片段,不喜跳过 很简单,快速过一下,F1,然后输入 snippets vue代码片段 { // Place your snippets for vue here ...

  9. luogu_2605: 基站选址

    洛谷2605:基站选址 题意描述: 有\(N\)个村庄在一条直线上,第\(i(i>1)\)个村庄的距离第\(1\)个村庄的距离为\(D_i\). 需要在这些村庄中建立不超过\(K\)个通讯站,在 ...

  10. ent 基本使用四 图遍历查询

    接上文,我们已经创建了基本的关系以及表实体,以下是通过图方式的查询 参考关系图 代码处理 创建图数据 func CreateGraph(ctx context.Context, client *ent ...