题目:

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. C++指针数组和数组指针

    指针相关问题 using namespace std; int main(){ //a) 一个整型数( An integer) int a; //b) 一个指向整型数的指针( A pointer to ...

  2. 总结:spring 的ioc 知识点

    1. spring ioc的优点    解耦,对象的创建不直接在代码里生成(不方便修改) ,交由容器创建.... 2.IOC 和 DI 的区别        依赖关系:A类 里 要调用B类  如 se ...

  3. js分页算法

    function get_hs_page(cur_page, total_page) { var result = ""; for(var i = 1; i <= total ...

  4. js文本框失去焦点事件与按钮单击事件的冲突问题

    <input type="text" id="txt" name="txt"/> <input type="bu ...

  5. Ubuntu 配置swftools(Ubuntu14.04)

    1.下载文件 wget http://swftools.org/swftools-0.9.0.tar.gz .tar.gz wget http://www.ijg.org/files/jpegsrc. ...

  6. Symfony2之创建一个简单的web应用

    Symfony2——创建bundle       bundle就像插件或者一个功能齐全的应用,我们在应用层上开发的应用的所有代码,包括:PHP文件.配置文件.图片.css文件.js文件等都会包含在bu ...

  7. AFC项目开发文档整理

    AFC项目开发文档整理 PHPCMS 的确是一个伟大的CMS,我对它爱不释手. 标签嵌套无法loop获取的解决办法.关键代码如下: /\*后台添加\*/ $str = preg_replace ( & ...

  8. 参数解析argparse模块

    argparse,python的一个命令行解析模块 import argparse #创建一个命令行解析器 parser = argparse.ArgumentParser() #增添参数 parse ...

  9. C语言(按键获取与函数)

    举一个简单的例子,如果有按键,就输出相关按键.否则,输出“.”.每隔 100 毫秒输出一次.按 ESC 退出.注:ESC 的 ASCII 码是 27. #include <stdio.h> ...

  10. linux内核学习之一:环境搭建--安装Debian7.3

    本系列文章假设读者已对linux有一定的了解,其实学习linux内核不需要有很深的关于linux的知识,只需要了解以下内容:linux基础知识及基本shell命令:现代操作系统的基本概念:C语言和gc ...