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. nginx 页面加载不全的问题

    在nginx的server中添加: proxy_buffer_size 2m; proxy_buffers 8 1m; proxy_busy_buffers_size 2m; 这是由于页面内容过长,默 ...

  2. python正则表达式(4)--search方法

    1.re.search函数 re.search 扫描整个字符串并返回第一个成功的匹配,如果匹配失败search()就返回None. (1)函数语法: re.search(pattern, string ...

  3. MoveIt简单编程

    目的:使用一些简单代码使机器人运动到指定位置.讲解代码怎么实现机器人的运动. 参考文献: 第一个博客需要下载<Mastering ROS for robotics Programming> ...

  4. P2220 [HAOI2012]容易题[小学数学]

    题目描述 为了使得大家高兴,小Q特意出个自认为的简单题(easy)来满足大家,这道简单题是描述如下: 有一个数列A已知对于所有的A[i]都是1~n的自然数,并且知道对于一些A[i]不能取哪些值,我们定 ...

  5. GC线程是不是守护线程

    是 线程的话分为守护线程和非守护线程(即用户线程) 只要当前JVM实例中尚存在任何一个非守护线程没有结束,守护线程就全部工作; 只有当最后一个非守护线程结束时,守护线程随着JVM一同结束工作,守护线程 ...

  6. 20180418模拟赛T1——Seq

    Seq (seq.cpp/c/pas) 题目描述 Description 木吉要去征讨VAN様,所以他现在需要从他身边的人中选出若干位陪同.现在有\(n\)个人站成一行,木吉要从其中选出\(2\)批在 ...

  7. python列表各种切片姿势

    顺着切,反着切,想怎么切就怎么切,但是别被坑. mylist = [1,2,3,4,5,6,7,8,9] print(mylist[2:7:2]) # [3, 5, 7] print(mylist[: ...

  8. typename T::SubType * ptr;

    #include <iostream> using namespace std; template<class T> class MyClass{ public: typena ...

  9. 聊聊CMSIS-RTOS是什么东东

    起因:发布自己翻译用的CMSIS_RTOS_Tutorial后,陆续收到网友关于“CMSIS-RTOS是干么的?”之类的问题,再次统一回复. 众所周知,实时操作系统是嵌入式领域的基石.而可选的嵌入式操 ...

  10. mybatis include refid="Base_Column_List"含义

    <sql id="Base_Column_List" > collegeID, collegeName </sql> <select id=" ...