Python3解leetcode Same Tree
2024-10-20 15:45:19
问题描述:
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
Example 1:
Input: 1 1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] Output: true
Example 2:
Input: 1 1
/ \
2 2 [1,2], [1,null,2] Output: false
Example 3:
Input: 1 1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] Output: false
思路:
考虑递归解法,将大问题化解为无数个类似的小问题
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
if p == None and q ==None:
return True
elif p == None or q ==None or p.val != q.val:
return False
if p.val == q.val :
return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
最新文章
- ASP.NET空网页生成默认代码注释
- Java中的List操作
- Android 中布局设置导致的TextView不显示的问题
- OSG动画学习
- linux 冒号的用途
- malloc函数和其他内存分配函数
- 转:Scrapy安装、爬虫入门教程、爬虫实例(豆瓣电影爬虫)
- 【Python】一个简单的例子
- border属性妙用
- inux xsel 拷贝复制命令行输出放在系统剪贴板上
- QT之深入理解QThread
- js 常用方法记事本
- 昨天面试遇到的一道C语言题
- Brief introduction to Java String Split 【简单介绍下Java String Split】
- Jquery table元素操作-创建|数据填充|重置|隐藏行
- tp5 查询单个字段的值
- c语言中字符串数组初始化的一点总结&;&; c++访问控制的三种方式
- IOT相关协议
- CSS兼容性(IE和Firefox)技巧
- C++动态(显式)调用 C++ dll
热门文章
- MySQL 5.7.18的安装及主从复制(主从同步)
- html 锚点定位
- nexus-2.11.4-01-bundle.tar.gz 下载地址
- jQuery学习笔记(7)--表格展开关闭
- 如果这种方式导致程序明显变慢或者引起其他问题,我们要重新思考来通过 goroutines 和 channels 来解决问题
- Brotli
- 一文看透 Redis 分布式锁进化史(解读 + 缺陷分析)(转)
- 流畅python学习笔记第十八章:使用asyncio包处理并发(二)
- global 全局变量的用法
- Android之Handler使用方法总结