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. Virtualbox 设置虚拟机和物理机共享文件夹

    Virtualbox 设置虚拟机和物理机共享文件夹 概述 当我们在本地机安装好一个虚拟机后,特别是安装linux系统的朋友们,经常需要将本地机的文件传递到虚拟机中, 能实现的方式肯定是多式多样的,就本 ...

  2. 100% 成功率的 offer 收割机是怎样练成的?

    都说今年的形势不好,各种找工作不顺利,但我身边就有一位同学,每次面试都拿到offer,我特意邀请他来给大家分享下经验,虽然不同人的技术领域未必相同,但很多东西是相通的,希望本文能对大家有所帮助. 下面 ...

  3. ArcGIS 将自定义工具发布到ArcGIS Server,作为Geoprocessing Service

    新建自定义工具过程可参考上一篇博客:http://www.cnblogs.com/oceanking/p/3933681.html 1.执行自定义工具.此步骤不可省略 打开ArcCatalog,双击自 ...

  4. 项目Beta冲刺--4/7

    项目Beta冲刺--4/7 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Beta冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及 ...

  5. 牛客NOIP暑期七天营-提高组6C:分班问题 (组合数)

    题意:A班有N个人,B班有M个人,现在要组成一个新的班级C班,为了公平,从AB班各抽相同人数的人. 现在求所有方案中,人数之和是多少. 思路:即求Σ k*C(N,k)*C(M,k);    先忽略这个 ...

  6. vim编辑时遇到E325: ATTENTION Found a swap file by the name "./.backu.sh.swp"错误代码的解决办法

    vim编辑时遇到E325: ATTENTION Found a swap file by the name "./.backu.sh.swp"错误代码的解决办法 重点:解决方法是: ...

  7. PHP - Filters

    Retrieve the administrator password of this application. 对文件包含的介绍------------第一个链接需要谷歌 https://mediu ...

  8. 强大的接口调试工具-Postman图文详解

    前言 在前后端分离开发时,后端工作人员完成系统接口开发后,需要与前端人员对接,测试调试接口,验证接口的正确性可用性.而这要求前端开发进度和后端进度保持基本一致,任何一方的进度跟不上,都无法及时完成功能 ...

  9. Xamarin.Android开发中遇到的问题

    开发 1.Resource.Id未包含xxx的定义 打开了一个OK的Id,是位于\obj\Debug\90\designtime\Resource.designer.cs ,打开文件搜索xxx,果然没 ...

  10. Fix multiple GPUs fails in training Mask_RCNN

    Test with: Keras: 2.2.4Python: 3.6.9Tensorflow: 1.12.0 ================== Problem: Using code from h ...