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

For example, this binary tree is symmetric:

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

But the following is not:

    1
/ \
2 2
\ \
3 3

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

1. 递归

bool isSymmetric(TreeNode *p, TreeNode *q){
if (p==NULL && q==NULL) return true;
if (p==NULL || q==NULL) return false; return (p->val == q->val) &&
isSymmetric(p->left, q->right) &&
isSymmetric(p->right, q->left);
}

2. 非递归

bool isSymmetric(TreeNode *p, TreeNode *q)
{
queue<TreeNode*> q1;
queue<TreeNode*> q2;
q1.push(p);
q2.push(q);
while(q1.size()> && q2.size()>){
TreeNode* p1 = q1.front();
q1.pop();
TreeNode* p2 = q2.front();
q2.pop();
if (p1==NULL && p2==NULL) continue;
if (p1==NULL || p2==NULL) return false; if (p1->val != p2->val) return false; q1.push(p1->left);
q2.push(p2->right); q1.push(p1->right);
q2.push(p2->left); }
return true;
}

101. Symmetric Tree -- 判断树结构是否对称的更多相关文章

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

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

  2. LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)

      思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...

  3. 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现

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

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

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

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

    给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称).例如,这个二叉树 [1,2,2,3,4,4,3] 是对称的.    1   / \  2   2 / \ / \3  4 4  3但是 ...

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

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

  7. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. &lt;LeetCode OJ&gt; 101. Symmetric Tree

    101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...

  9. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

随机推荐

  1. 2.wireshark分析之TCP协议(一)

    (1) TCP是怎么样的协议? TCP是一种面向连接(连接导向)的.可靠的基于字节流的传输层通信协议.TCP将用户数据打包成报文段,它发送后启动一个定时器,另一端收到的数据进行确认.对失序的数据重新排 ...

  2. python 的 ord()、 chr()、 unichr() 函数

    一. ord() 函数描述ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返 ...

  3. 比特币 BTC本地测试节点

    BTC本地测试节点 #环境 ubuntu 16.4 #客户端安装 #下载页面 #https://bitcoin.org/zh_CN/download cd /opt/ wget https://bit ...

  4. ERROR 2002 (HY000): Can't connect to local MySQL server through socket

    原文链接:https://blog.csdn.net/u011262253/article/details/82802157 一.错误现场还原: 下面我们通过三种方式来连接,然后观察提示的错误信息: ...

  5. 由SOAP说开去 - - 谈谈WebServices、RMI、RPC、SOA、REST、XML、JSON

    引子: 关于SOAP其实我一直模模糊糊不太理解,这种模模糊糊的感觉表述起来是这样: 在使用web服务时(功能接口),本来我就可以通过安卓中固有的http类(使用http协议),来发送http请求,并且 ...

  6. 爬虫基础 - Robots协议

    Robots协议 指定一个robots.txt文件,告诉爬虫引擎怎么爬取 https://www.taobao.com/robots.txt User-agent: Baiduspider Allow ...

  7. Django小项目简单BBS论坛

    开发一个简单的BBS论坛 项目需求: 1 整体参考"抽屉新热榜" + "虎嗅网" 2 实现不同论坛版块 3 帖子列表展示 4 帖子评论数.点赞数展示 5 在线用 ...

  8. http之http1.0和http1.1的区别

    下面主要从几个不同的方面介绍HTTP/1.0与HTTP/1.1之间的差别,当然,更多的内容是放在解释这种差异背后的机制上. 1 可扩展性 可扩展性的一个重要原则:如果HTTP的某个实现接收到了自身未定 ...

  9. Codeforces Round #528 Solution

    A. Right-Left Cipher Solved. 注意长度的奇偶 #include <bits/stdc++.h> using namespace std; string s; i ...

  10. SQLServer中char、varchar、nchar、nvarchar比较

    转自:http://www.cnblogs.com/bluesky_blog/archive/2009/07/31/1535722.html 对于程序中的string型字段,SQLServer中有ch ...