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 ...
随机推荐
- Android-Animations介绍
一.Animations介绍 Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转.缩放.淡入淡出等,这些效果可以应用在绝大 ...
- hadoop修改主机名遇到的坑
正确的修改方式 CentOS修改主机名(hostname) 需要修改两处:一处是/etc/sysconfig/network,另一处是/etc/hosts,只修改任一处会导致系统启动异常.首先切换到r ...
- 如何运用spring将dao注入到servlet中?
1.servlet的init方法 public void init(ServletConfig config) throws ServletException { super.init(config) ...
- swift基础教程笔记
http://www.imooc.com/learn/127 <玩儿转swift> 慕课网教程笔记,自己根据2.1的语法做了更新. I. 1.通过playground来学习.熟悉swift ...
- 转载 ---资深HR告诉你:我如何筛选简历与选择人员的
资深HR告诉你:我如何筛选简历与选择人员的 有个公司HR看简历 先直接丢掉一半 理由是不要运气不好的应聘者. 当然这可能只是某些HR面对太多的简历产生了偷懒的情绪,但是不论是Manager,亦或是 ...
- JVM相关小结
对JVM中分层模型.垃圾回收期.垃圾回收算法趁着周末小结一下.有不对的地方,还请指正和讨论~ 1.JVM内存模型 2.JVM垃圾回收期 3.JVM垃圾回收算法 ------------------- ...
- 【转】soapUI和Jmeter的接口测试结构区别
使用SoapUI和Jmeter都可以进行自动化接口测试,但是每个工具都有自身的特点,所以他们的结构也有一定的区别 SoapUI 项目名称 -Rest服务.Rest资源 在使用SoapUI进行接口测试时 ...
- myeclipse中disable maven nature怎么恢复
eclipse更新maven的时候,不小心手一抖,点上了Disable Maven Nature,然后工程右键菜单中的Maven栏就不见了! 其实这是把maven工程转换成了一般工程,再转回来就好了. ...
- git merge的本质
1 git merge [branch] 将[branch]这个分支merge到当前分支. 2 merge的本质 merge就是把branch上的提交合入当前分支的提交树,这两个分支上的所有提交的历史 ...
- python cookbook第三版学习笔记七:python解析csv,json,xml文件
CSV文件读取: Csv文件格式如下:分别有2行三列. 访问代码如下: f=open(r'E:\py_prj\test.csv','rb') f_csv=csv.reader(f) for f in ...