Python3解leetcode Symmetric Tree
2024-09-07 18:06:47
问题描述:
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)
最新文章
- Android 全局获取 Context 与使用 Intent 传递对象
- HTML5新的标签和属性
- oracle杀用户建用户改密码脚本
- cocos代码研究(1)sprite学习笔记
- web网页的表单排版利器--960css
- DTCMS自定义标签:获取所有栏目以及不显示指定栏目
- KeilC51使用详解 (三)
- Java 数据类型转换(转换成字节型)
- [C入门 - 游戏编程系列] 贪吃蛇篇(一) - 世界定义
- Linux下 目录 压缩 解压缩 打包
- 使用光盘iso实现Linux操作系统的自动安装部署
- Android 开发笔记___alertDialog
- Python05(运算符)
- JS Function类型
- [GAN] Generative networks
- PHP学习目标
- PHP Fatal error: Call to undefined function mysql_connect() 错误解释
- asp.net导出excel并弹出保存提示框
- 用turtle库显示汉诺塔问题的过程
- find . -mtime +1 查找文件
热门文章
- virtualbox 4.3.10 ubuntu 12.04 mount share folder bug
- Rider
- KVM+VNC 虚拟机远程管理
- 【WPF学习笔记】之如何设置下拉框读取SqlServer数据库的值:动画系列之(一)
- python 基础 5.0 python类一般形式
- 【BZOJ2427】[HAOI2010]软件安装 Tarjan+树形背包
- pgsql 数据类型
- 性能测试--测试流程、APDEX、linux性能知识
- 【python】使用python写windows服务
- .idea文件夹是干嘛的