题目:

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

刷题打卡中。

。。。

Same Tree 与昨天所做的Maximum depth 的思路一致,採用函数递归调用的方法。两个二叉树全然同样的条件即是:根节点值同样 and 左子树同样 and 右子树同样

上代码咯

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param p, a tree node
# @param q, a tree node
# @return a boolean
def isSameTree(self, p, q):
if p==None or q==None:
if p==None and q==None:
return True
else:
return False
else:
if p.val==q.val and self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right):
return True
else:
return False

补充更新~~~

c++里的true和false为小写。而python里为大写


Leetcode_num3_Same Tree的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  3. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. Eclipse,hadoop2.7.2 hadoop-eclipse-plugin.jar的制作

    装好了hadoop后发现有装个eclipse的必要,于是参照文章A(http://www.powerxing.com/hadoop-build-project-using-eclipse/)进行安装, ...

  2. No suitable authentication method found to complete authentication (publickey,keyboard-interactive).

    string command = Command.Text; StringBuilder result = new StringBuilder(); try { var connectionInfo ...

  3. 找两个string[ ]里不同的元素

    方法 1:string[] strListOne = new string[] {"Type", "Parent Id","Status"} ...

  4. POJ 1159

    区间dp 比如从i到j如果str[i] == str[j]那么i到j的值就与i+1到j-1相同,否则从i到j-1与i+1到j中选一个最优解,这道题直接定义int的二维数组会mle,用short,也可以 ...

  5. linux下enum的使用

    enum T { status1, status2, } Linux下: 1.做函数返回值时enum T f():不能写成T f(): 2.if(i == status1)不能写成 if(i == T ...

  6. Linux中yum手动安装、手动建立仓库文件夹关联实现关联包自动安装、yum相关命令使用

               yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指 ...

  7. mysql中取系统当前时间

    <select id="getFreightEfclInventoryList" parameterType="long" resultMap=" ...

  8. Zepto源码笔记(三)

    ps:本文中"组装成成数组"指的是若元素个数大于1则返回数组,若元素只有1个则返回元素本身 以下函数是$.fn该对象的方法 ready(callback) 通过readyRE正则表 ...

  9. MSIL

    公共字段Add 将两个值相加并将结果推送到计算堆栈上. Add_Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add_Ovf_Un 将两个无符号整数值相加,执行溢出检查,并且将结 ...

  10. css 画竖着线条

    <p>table控制:<!-- height为横线的粗度,width为长度 --><table width=700>     <tr>    <t ...