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==nullptr)
return true;
return helper(root->left,root->right);
}
bool helper(TreeNode* left,TreeNode* right)
{
if(!left&&!right)
return true;
else if(!left||!right)
return false;
else if(left->val!=right->val)
return false;
else
return helper(left->left,right->right)&&helper(left->right,right->left);
}
};

LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树的更多相关文章

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

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

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

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

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

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

  4. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  5. 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...

  6. (二叉树 DFS 递归) leetcode 101. Symmetric Tree

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

  7. LeetCode 101. Symmetric Tree (对称树)

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

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

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

  9. leetcode 101 Symmetric Tree ----- java

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

随机推荐

  1. mysql基础之三:char与varchar区别,varchar(M)能存多少

    char与varchar区别 char (13)长度固定, 如'1234567890' 存储需要空间 10个字符; varchar(13) 可变长 如'1234567890' 需要存储空间 11字符; ...

  2. 2 ubuntu 16.04 安装Elastic Stack

    一: 安装JAVA8          添加ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 安装oracle- ...

  3. Python-RabbitMQ消息队列实现rpc

    客户端通过发送命令来调用服务端的某些服务,服务端把结果再返回给客户端 这样使得RabbitMQ的消息发送端和接收端都能发送消息 返回结果的时候需要指定另一个队列 服务器端 # -*- coding:u ...

  4. pyodbc简单使用

    1.连接数据库 1)直接连接数据库和创建一个游标(cursor) cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABAS ...

  5. final注意事项

    final修饰的类为终态类,不能被继承,而 抽象类是必须被继承的才有其意义的,因此,final是不能用来修饰抽象类的. final修饰的方法为终态方法,不能被重写.而继承抽象类,必须重写其方法. 抽象 ...

  6. C# 中out 参数 和 ref参数的区别

    C#中共有4种参数类型,分别是 传值(by value), 传址 (by reference), 输出参数 (by output), 数组参数 (by array) by value => 传值 ...

  7. 15. Bypass 360主机卫士SQL注入防御(多姿势)

    在服务器客户端领域,曾经出现过一款 360 主机卫士,目前已停止更新和维护,官网都打不开了,但服务器中依然经常可以看到它的身影. 从半年前的测试虚拟机里面,翻出了 360 主机卫士 Apache 版的 ...

  8. SqlServer2012-创建表、删除表 增加字段 删除字段操作

    新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) default \'默认值\' nu ...

  9. opencv使用findContours等方法出现内存损坏之类的不能调用问题

    错误现象: 编译项目,如果该项目是debug,则在链接器输入配置opencv_world310d.lib如果是release则输入opencv_world310.lib,输入其中一个,多输,输错运行不 ...

  10. React 从入门到进阶之路(三)

    之前的文章我们介绍了 React 创建组件.JSX 语法.绑定数据和绑定对象.接下来我们将介绍 React 绑定属性( 绑定class  绑定style).引入图片  循环数组渲染数据. 上一篇中我们 ...