问题描述:

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.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if root == None:
return True
def ismirror(root1,root2):
if root1 == None and root2 ==None:
return True
elif root1 == None or root2 ==None or root1.val != root2.val:
return False
return ismirror(root1.left,root2.right) and ismirror(root1.right,root2.left) return ismirror(root.left,root.right)

最新文章

  1. Android 全局获取 Context 与使用 Intent 传递对象
  2. HTML5新的标签和属性
  3. oracle杀用户建用户改密码脚本
  4. cocos代码研究(1)sprite学习笔记
  5. web网页的表单排版利器--960css
  6. DTCMS自定义标签:获取所有栏目以及不显示指定栏目
  7. KeilC51使用详解 (三)
  8. Java 数据类型转换(转换成字节型)
  9. [C入门 - 游戏编程系列] 贪吃蛇篇(一) - 世界定义
  10. Linux下 目录 压缩 解压缩 打包
  11. 使用光盘iso实现Linux操作系统的自动安装部署
  12. Android 开发笔记___alertDialog
  13. Python05(运算符)
  14. JS Function类型
  15. [GAN] Generative networks
  16. PHP学习目标
  17. PHP Fatal error: Call to undefined function mysql_connect() 错误解释
  18. asp.net导出excel并弹出保存提示框
  19. 用turtle库显示汉诺塔问题的过程
  20. find . -mtime +1 查找文件

热门文章

  1. virtualbox 4.3.10 ubuntu 12.04 mount share folder bug
  2. Rider
  3. KVM+VNC 虚拟机远程管理
  4. 【WPF学习笔记】之如何设置下拉框读取SqlServer数据库的值:动画系列之(一)
  5. python 基础 5.0 python类一般形式
  6. 【BZOJ2427】[HAOI2010]软件安装 Tarjan+树形背包
  7. pgsql 数据类型
  8. 性能测试--测试流程、APDEX、linux性能知识
  9. 【python】使用python写windows服务
  10. .idea文件夹是干嘛的