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(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if p and q:
return p.val==q.val and self.isSameTree(p.left,q.left) and self.isSameTree(q.right,p.right)
return p is q

  

[LeetCode&Python] Problem 100. Same Tree的更多相关文章

  1. [LeetCode&Python] Problem 101. Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  2. [LeetCode&Python] Problem 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  3. [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  4. [LeetCode&Python] Problem 563. Binary Tree Tilt

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...

  5. [LeetCode&Python] Problem 429. N-ary Tree Level Order Traversal

    Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  6. [LeetCode&Python] Problem 589. N-ary Tree Preorder Traversal

    Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary  ...

  7. [LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

  8. <LeetCode OJ> 100. Same Tree

    100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...

  9. LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

    100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; ...

随机推荐

  1. js正则匹配以某字符串开始字符串

    let decode_sql ="select * from table where create_user='user' order by id desc";   decode_ ...

  2. 使用Spring-data-jpa(1)(三十)

    在实际开发过程中,对数据库的操作无非就“增删改查”.就最为普遍的单表操作而言,除了表和字段不同外,语句都是类似的,开发人员需要写大量类似而枯燥的语句来完成业务逻辑. 为了解决这些大量枯燥的数据操作语句 ...

  3. Win10系列:VC++文件选取

    在C++/CX的Windows::Storage::Pickers命名空间中定义了一个FileOpenPicker类,使用此类可以新建一个文件打开选取器,并可以通过这个类里面包含的属性和函数选取一个或 ...

  4. spring boot 打包可以运行,但是执行main方法不能运行

    报错信息如下 2017-10-23 15:16:09.750 ERROR 7664 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReport ...

  5. js图片时间和倒计时

    图片时间原理        原理:使用定时器每秒获取时间,获取时间的时,分,秒,组成一个6位数的字符串,然后用charAt,截取出对应的字符,图片命名和数字相对应就ok拉 倒计时原理        原 ...

  6. java局部变量,成员变量在堆和栈中的存储

    对于局部变量,如果是基本类型,会把值直接存储在栈:如果是引用类型,比如String s = new String("william");会把其对象存储在堆,而把这个对象的引用(指针 ...

  7. Grafana配置SingleStat图表信息(三)

    Grafana是一个用于展示数据的工具,配置数据源,直接连接数据库.(这里的数据库应该是结果库,直接需要给用户看的结果数据信息) SingleStat : 想用来展示单一数据信息的图表,效果图如图 ( ...

  8. Java实现将数字转为大写汉字

    public class Int2Big { static String int2big(int src) { final String num[] = {"零", "壹 ...

  9. vue-7-表单

    示例: <input v-model="message" placeholder="edit me"> <p>Message is: { ...

  10. java编写本月日历

    代码如下: import java.time.*; public class Main { public static void main(String arg[]){ LocalDate date ...