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. idea上传项目到github出现"remote with selected name already exists"情况的解决

    1. 2. 3.

  2. leetCodelinked-list-cycle-ii找到链表的环

    题目 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. N ...

  3. Yii2 Restful api设计--App接口编程

    Yii2框架写一套RESTful风格的API,对照魏曦教你学 一,入门 一.目录结构 实现一个简单地RESTful API只需用到三个文件.目录如下: frontend ├─ config │ └ m ...

  4. 扩展kmp 模板

    算法可以参考http://wenku.baidu.com/view/8e9ebefb0242a8956bece4b3.html 百度文库 #include<iostream> #inclu ...

  5. Boss Bo (主席树)

    主要想法:假设给你足够时间,那么就可以对每个点建议一颗线段树来查询了,但是需要将点全部按照某个特定的序列存进线段树,如代码是以树的深搜顺序作为指定顺序,这样以来我们既可以将数据查询分成诺干个区间进行查 ...

  6. 使用函数式编程消除重复无聊的foreach代码(Scala示例)

    摘要:使用Scala语言为例,展示函数式编程消除重复无聊的foreach代码. 难度:中级 概述 大多数开发者在开发生涯里,会面对大量业务代码.而这些业务代码中,会发现有大量重复无聊的 foreach ...

  7. nodejs之pm2自动重启服务

    pm2 start xxx #启动服务器 pm2 list #查看运行状态 pm2 logs #查看日志 pm2 restart xxx #重启应用 pm2 stop xxx #停止应用 监听修改,并 ...

  8. linux dns

    linux 用户相关的 root   相当于QQ群主 sudo  QQ群管理员 普通用户  QQ群水军 root  UID 是 0   组UID也是0  普通用户UID从1000开始 查看用户id 信 ...

  9. Python+OpenCV图像处理(九)—— 模板匹配

    百度百科:模板匹配是一种最原始.最基本的模式识别方法,研究某一特定对象物的图案位于图像的什么地方,进而识别对象物,这就是一个匹配问题.它是图像处理中最基本.最常用的匹配方法.模板匹配具有自身的局限性, ...

  10. Django框架----用户认证auth模块

    一.auth模块 auth模块:针对auth_user表 创建超级管理用户命令: Python manage.py createsuperuser添加用户名添加密码(至少8位)确认密码添加邮箱(可为空 ...