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 思路基本的DFS, 都空, 为True, 都不为空, 判断value一样, 并且recursive children, 否则False 1. Constraints
1) 可以都为None => True 2. Ideas DFS O(n) 3. Code
class Solution:
def sameTree(self, p, q):
if not p and not q:
return True
if p and q and p.val == q.val:
return self.sameTree(p.left, q.left) and self.sameTree(p.right, q.right)
return False

[LeetCode] 100. Same Tree_Easy tag: DFS的更多相关文章

  1. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  2. [LeetCode] 559. Maximum Depth of N-ary Tree_Easy tag: DFS

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  3. [LeetCode] 110. Balanced Binary Tree_Easy tag: DFS

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  4. [LeetCode] 111. Minimum Depth of Binary Tree_Easy tag:DFS

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. [LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. [LeetCode] 872. Leaf-Similar Trees_Easy tag: DFS

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form ...

  7. [LeetCode] 721. Accounts Merge_Medium tag: DFS recursive

    Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...

  8. [LeetCode] 112. Path Sum_Easy tag: DFS

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  9. [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

随机推荐

  1. day_5.19 py总结②

  2. I - A/B

    要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input 数据的第一行是一个T,表示有T组数据. 每组数据有 ...

  3. C#学习之委托和事件

    C#学习中,关于委托和事件的一些见解: 一.C语言中的函数指针 想要理解什么是委托,就要先理解函数指针的概念.所谓函数指针,就是指向函数的指针(等于没说-.-).比如我定义了两个函数square和cu ...

  4. db2 reorg到底需要多少表空间(转)

    脱机reorg需要一定的空间,这个空间与目标所在的数据表空间.索引表空间.以及临时表空间均有关,各空间需求的大小与表和索引所占用的数据页和索引页相关. (1)对表执行reorg操作如:db2 reor ...

  5. svn的安装方法

    SVN的安装很常用.但是我好像经常用了之后就忘记.这次把笔记放在这里,如果以后忘了,就可以随时查看了. 步骤: 一.下载SVN 这里常用的是site.zip. 之前是手头有现成的site.zip.今天 ...

  6. 基础知识系列☞关键字→virtual

    看到自己以前的一篇博客,感觉还差点什么,√,代码... using System; namespace Test { public class Program { private static voi ...

  7. MYSQL数据库在Windows系统中重置root密码

    1.以系统管理员身份打开cmd 2.查看MySQL是否启动: net start 3.如果启动就停止:net stop MySQL55 注:MySQL55是在我的电脑上的mysql数据库服务名 4.再 ...

  8. [No0000BB]ReSharper操作指南4/16-配置ReSharper代码快修与导航

    代码问题的快速修复 ReSharper可以帮助您立即修复设计时检测到的大部分代码问题.就像按Alt+Enter突出显示的代码问题一样简单,并选择合适的方法来解决问题或改进次优代码. GIF 应用快速修 ...

  9. Impala2.7.0-cdh5.x.x安装部署

    部署impala impala安装选择rpm包方式进行,这是本次部署唯一一个主要主件采用rpm方式进行安装部署,这里主要原因是cloudera没有提供现成的tar包文件,而源码编译过程会出现各种未知原 ...

  10. Page11:状态反馈、输出反馈的概念及性能比较,极点配置的基本概念、意义及其算法[Linear System Theory]

    内容包含离散时间线性时不变系统的稳定判据 状态反馈.输出反馈的基本概念及其性能比较 极点配置的基本概念.意义及其算法