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. c#时间与时间戳互转13位

    Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00 ...

  2. V-rep(1)

    第一次课堂作业,需要导入网格三维模型,对齐坐标系,然后在各个关节添加jiont,实现外观模型和运动仿真模型的分离. 1.首先导入模型.导入模型可能是一个整体模型(装配好的),也可能是单个(mesh)网 ...

  3. Mac Docker安装Redis4.0

    mkdir redis 在~目录下创建redis目录 docker run -d -p 6379:6379 -v $PWD/redis:/data -d --name redis4.0 redis:4 ...

  4. php装饰器模式(decorator pattern)

    十一点了. <?php /* The decorator pattern allows behavior to be added to an individual object instance ...

  5. deque_queue_list

    #include <iostream> #include <deque>//front push pop back push pop [] at() #include < ...

  6. 在WSL中安装zsh终端

    cmder中设置: 新增task,名称为wsl::bash,右下方的方框内填写: "%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBa ...

  7. 数据库join解释 与视图

    数据库的视图是表运算的结果. 数据库的表是数据单元: join是运算符: 视图是运算结果. 数据库join解释 1.join:将两个表结构连接成一个视图 2.left.right.inner: 从基准 ...

  8. continue语句:编程把100-300之间的能被25整除的数输出

    #include<stdio.h>void main(){ int n; for(n=100;n<=300;n++) { if(n%25!=0) continue; printf(& ...

  9. C# 请求数据 方式1

    public static string PostWebRequest2() { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create( ...

  10. 20101010 exam

    目录 2018 10.10 exam 解题报告 T1:LOJ #10078 新年好 题目描述(原题来自:CQOI 2005): 输入格式: 输出格式: 样例输入: 样例输出: 数据范围与提示: 思路: ...