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. linux命令目录

    一.文件和目录.(文件目录的增删改查) ls pwd cd mkdir touch rmdir ln dd rm cp mv nl cat tac more less head tail stat # ...

  2. 软工网络15团队作业4——Alpha阶段敏捷冲刺5.0

    1.每天举行站立式会议,提供当天站立式会议照片一张. 2.项目每个成员的昨天进展.存在问题.今天安排. 成员 昨天已完成 今天计划完成 郭炜埕 完善新建话题界面 实现前端各界面的跳转连接 郑晓丽 进行 ...

  3. python django简单的登陆实现

    实现方法: 1,可以先定义一个基础的页面访问路径 例如:http://127.0.0.1:8000/index/  定义index路径 在urls urlpatterns = [ url(r'^ind ...

  4. Spring 无缝整合 quartz

    关键步骤: 1. 配置 SchedulerFactoryBean <bean class="org.springframework.scheduling.quartz.Schedule ...

  5. Java volatile详解

    转自:http://www.cnblogs.com/dolphin0520/p/3920373.html volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受 ...

  6. POJ 3624 Charm Bracelet (01背包)

    题目链接:http://poj.org/problem?id=3624 Bessie has gone to the mall's jewelry store and spies a charm br ...

  7. node 按行读取文件

    var readline = require('readline'); var fs = require('fs'); var os = require('os'); var fReadName =  ...

  8. IP代理

    import requests # 定义爬取url地址 base_url = 'https://www.baidu.com/' # 定义代理IP地址 proxies = {'http':'http:/ ...

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

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

  10. spring框架入门之一

    一.什么是Spring框架 1.什么是Spring Spring框架是个一个全栈的框架.意思就是使用Spring的框架可以开发web层,service层还有dao层. 本质:Spring就是一个对象的 ...