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. WPF使用Canvas绘制可变矩形

    1.问题以及解决办法 最近因为项目需要,需要实现一个位置校对的功能,大致的需求如下:有一个图片,有一些位置信息,但是位置信息可能和实际有些偏差,需要做简单调整,后面会对这张图片进行切割等,做些处理.( ...

  2. shell特殊变量,记录一下

    $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数是$1,第二个参数是$2. $# 传递给脚本或函数的参数个数. $* 传递给脚本或函数的所有参数. ...

  3. Xcode编译警告Assigning to 'id<XXXDelegat> ——Nullable' from incompatible type 'XXXView *const_strong'

    编译报警告 可能是 自定义分类使用协议时出现与父类协议的冲突 解决方法如下:    

  4. 【Java】一台服务器配置多个Tomcat

    需求缘由 最近接收了一个新的工具业务:ipublish发布系统,刚接手这个业务的时候,发现每次发布新的代码 需要到群里告知大家,我要停服务几分钟,准备更新代码啦.这尼玛 哪个公司都不敢这么牛逼的和用户 ...

  5. scala-数组/列表

    import scala.collection.mutable.ArrayBuffer var ary=Array(1,2,3) println(ary.mkString) println(ary(1 ...

  6. 20165213&20165225结对学习感想及创意照

    20165213&20165225结对学习感想及创意照 会JAVA的大学生活好小组 团队感悟: 1+1>2还是1+1<2? 上述两个观点实际没有对错之分,取决点在于个人见解. 相信 ...

  7. Flask需要登录权限的装饰器写法

    def wapper(func): def inner(*args,**kwargs): if not request.cookies.get("username"): retur ...

  8. Servlet (二)ServletContext

    package cn.sasa.serv; import java.io.IOException; import javax.servlet.ServletContext; import javax. ...

  9. 【Python爬虫】Requests库的基本使用

    Requests库的基本使用 阅读目录 基本的GET请求 带参数的GET请求 解析Json 获取二进制数据 添加headers 基本的POST请求 response属性 文件上传 获取cookie 会 ...

  10. VB改写C#

    1.VB的Val()函数 先从程序集中引入Microsoft.VisualBasic命名空间.不过,即便是引入了Microsoft.VisualBasic命名空间,还是不能直接使用像Val()这样的函 ...