Python3解leetcode Same Tree
问题描述:
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)
Python3解leetcode Same Tree的更多相关文章
- Python3解leetcode N-ary Tree Level Order Traversal
问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- Python3解leetcode Symmetric Tree
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- Python3解leetcode Binary Tree Paths
问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...
- Python3解leetcode Binary Tree PathsAdd DigitsMove Zeroes
问题描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...
- Python3解leetcode Binary Tree PathsAdd Digits
问题描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- Python3解leetcode Same TreeBinary Tree Level Order Traversal II
问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- Python3解leetcode Average of Levels in Binary Tree
问题描述: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...
- Python3解leetcode Subtree of Another Tree
问题描述: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
- Python3解leetcode Lowest Common Ancestor of a Binary Search Tree
问题描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...
随机推荐
- Appium python Uiautomator2 多进程问题
appium更新uiautomator后可以获取tost了,大家都尝试,课程中也讲解了,但是这些跑的时候都在单机上,当我们多机并发的时候会出现一个端口问题,因为我们appium最后会调用uiautom ...
- 深入Asyncio(十一)优雅地开始与结束
Startup and Shutdown Graceful 大部分基于asyncio的程序都是需要长期运行.基于网络的应用,处理这种应用的正确开启与关闭存在惊人的复杂性. 开启相对来说更简单点,常规做 ...
- ndk javah配置
Location: C:\Program Files\Java\jdk1.6.0_25\bin\javah.exe Working Directory: ${project_loc} Argument ...
- 【BZOJ2132】圈地计划 最小割
[BZOJ2132]圈地计划 Description 最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地. ...
- VI带行号查看
:set nu 带行号查看,并不改变文件内容 :set nonu 取消带行号查看 在每个用户的主目录下,都有一个 vi 的配置文件".vimrc"或 ...
- Complete space 完备空间与柯西序列 巴拿赫空间与完备空间 完备空间与和希尔伯特空间 封闭closed与完备性complete
http://www.gatsby.ucl.ac.uk/~gretton/coursefiles/RKHS2013_slides1.pdf RKHS: a function space with a ...
- 基于GeoEvent Processor的物联网应用案例赏析
1 技术路线 下面全部应用,都採用ArcGIS for Server,结合GeoEvent产品(为一款物联网实时数据集成处理产品)开发完毕. 2 应用场景 1.1 物联网实时态势感知 1.1.1 ...
- PAT 天梯赛 L2-025. 分而治之 【图】
题目链接 https://www.patest.cn/contests/gplt/L2-025 思路 只要把被攻下的城市标记一下 与 其他城市之间的通路都取消 然后判断一下剩下的城市 是否都是孤立的 ...
- ajax进度条
.graph { width: 400px; height: 25px; border: 1px solid #f8b3d0; } .bar { background-color: #ffe7f4; ...
- :style动态设置属性
前段时间做页面时需要动态设置背景图片,每一种框架都会遇见类似的需求,特记录下来,以免不时之需: <!DOCTYPE html> <html> <head> < ...