问题描述:

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)

最新文章

  1. ASP.NET空网页生成默认代码注释
  2. Java中的List操作
  3. Android 中布局设置导致的TextView不显示的问题
  4. OSG动画学习
  5. linux 冒号的用途
  6. malloc函数和其他内存分配函数
  7. 转:Scrapy安装、爬虫入门教程、爬虫实例(豆瓣电影爬虫)
  8. 【Python】一个简单的例子
  9. border属性妙用
  10. inux xsel 拷贝复制命令行输出放在系统剪贴板上
  11. QT之深入理解QThread
  12. js 常用方法记事本
  13. 昨天面试遇到的一道C语言题
  14. Brief introduction to Java String Split 【简单介绍下Java String Split】
  15. Jquery table元素操作-创建|数据填充|重置|隐藏行
  16. tp5 查询单个字段的值
  17. c语言中字符串数组初始化的一点总结&& c++访问控制的三种方式
  18. IOT相关协议
  19. CSS兼容性(IE和Firefox)技巧
  20. C++动态(显式)调用 C++ dll

热门文章

  1. MySQL 5.7.18的安装及主从复制(主从同步)
  2. html 锚点定位
  3. nexus-2.11.4-01-bundle.tar.gz 下载地址
  4. jQuery学习笔记(7)--表格展开关闭
  5. 如果这种方式导致程序明显变慢或者引起其他问题,我们要重新思考来通过 goroutines 和 channels 来解决问题
  6. Brotli
  7. 一文看透 Redis 分布式锁进化史(解读 + 缺陷分析)(转)
  8. 流畅python学习笔记第十八章:使用asyncio包处理并发(二)
  9. global 全局变量的用法
  10. Android之Handler使用方法总结