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.

 
需要左右对称,解题思路和Same Tree类似。
如果拿掉root结点的话,逻辑其实相当于是比较root.left和root.right是否same tree类似的逻辑。只是需要把之前的left.left和right.right比较,left.right和right.left进行比较。
 
 public bool IsSymmetric(TreeNode root)
{
return IsMirror(root?.left, root?.right);
} public bool IsMirror(TreeNode left, TreeNode right)
{
bool flag;
if (left == null && right == null)
{
flag = true;
}
else if (left == null || right == null)
{
flag = false;
}
else
{
if (left.val == right.val)
{
flag = IsMirror(left.left, right.right) && IsMirror(left.right, right.left);
}
else
{
flag = false;
}
} return flag;
}
 
 

101. Symmetric对称 Tree的更多相关文章

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

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

  2. <LeetCode OJ> 101. Symmetric Tree

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

  3. Leetcode之101. Symmetric Tree Easy

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

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

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

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

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

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

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

  7. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  8. [Leetcode 101]判断对称树 Symmetric Tree

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

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

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

随机推荐

  1. 电脑已连接wifi的密码查询

    有时候,想登陆自己家的无线网络(尤其朋友来家里突然要连接无线网络),脑子刹那间一片空白想不起来密码,怎么办呢? 其实,我们可以通过电脑来查看网络的密码,现在分享如何在笔记本电脑上查看连接过的无线网络密 ...

  2. 联想拯救者win10+ubuntu14.04

    之前是win10+win7,默认win10启动,win7所在磁盘直接格式化即可(如果是win10+unbuntu不能这么搞,据说会导致win10也起不来) 按F2进bios 除了按这里fast boo ...

  3. 2.scrapy安装

    A.Anaconda如果已安装,那么可以通过 conda 命令安装 Scrapy,安装命令如下: conda install Scrapy   ============================ ...

  4. python SQLite说一点点, python使用数据库需要注意的几点

    SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成. Python就 ...

  5. python csv文件转换成xml, 构建新xml文件

    csv文件 code from xml.etree.ElementTree import Element,ElementTree,tostring import json,csv def csvtox ...

  6. python中__call__()方法的用法

    __call__()的用法 __call__()方法能够让类的实例对象,像函数一样被调用: >>> >>> class A(object): def __call_ ...

  7. 如何删除自己上传的CSDN资源(亲测有效)

    今天发现有一个资源上传错了,想重新上传,删掉以前的资源,才发现CSDN并没有提供删除资源的功能,然后去网上搜了下,这才删除了,不知道怎么删除的小伙伴看过来~ 1.首先,找到自己想要删除资源的页面,举个 ...

  8. GoldenGate 12.2抽取Oracle 12c多租户配置过程

    linux下安装12c 重启linux之后,dbca PDB/CDB使用 SQL> select instance_name from v$instance; INSTANCE_NAME --- ...

  9. 如何解决win10关机状态下,按键盘会自动开机的问题

    关机状态下按下键盘会自动开机,是因为所装的系统默认设置了快速启动功能 下面是关闭快速启动的方法: 步骤一: 在win10桌面右击,点击显示设置 步骤二: 电源和睡眠-->其他电源设置 步骤三: ...

  10. 流媒体协议(RTMP、RTSP、UDP、HTTP、MMS)转换小工具(RTSP转成RTMP案例展示)(转)

    源: 流媒体协议(RTMP.RTSP.UDP.HTTP.MMS)转换小工具(RTSP转成RTMP案例展示)