Same Tree

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.

Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

SAME TREE
'''
SAME TREE
Created on Nov 13, 2014

@author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
'''
# 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):
        self.__init__()
        self.foreachNode(p)
        seqP=self.seq

        self.__init__()
        self.foreachNode(q)
        seqQ=self.seq

        return seqP==seqQ

    def __init__(self):
        self.seq=[]

    def foreachNode(self, node):
        if(node==None): return
        self.seq.append(node.val)
        self.foreachNode(node.left)
        self.foreachNode(node.right)
MAX DEPTH
'''
MAX DEPTH
Created on Nov 13, 2014

@author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
'''
class Solution:
    # @param root, a tree node
    # @return an integer
    def maxDepth(self, root):
        self.__init__()
        self.foreachNode(root)
        return self.max

    def __init__(self):
        self.depth=0
        self.max=0

    def foreachNode(self, node):
        if(node==None): return 

        self.depth+=1
        if(node.left==None and node.right==None):
            if(self.depth>self.max):
                self.max=self.depth
        self.foreachNode(node.left)
        self.foreachNode(node.right)
        self.depth-=1

if __name__ == '__main__':
    pass
 

LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]的更多相关文章

  1. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

  2. 【Leetcode】【Easy】Maximum Depth of Binary Tree

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

  3. 【leetcode刷题笔记】Maximum Depth of Binary Tree

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

  4. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

  5. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  6. [LeetCode]题解(python):104 Maximum Depth of Binary Tree

    题目来源 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maxim ...

  7. 【LeetCode】【Python题解】Single Number &amp; Maximum Depth of Binary Tree

    今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的.由于c++版的代码网上比較多.所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做Lee ...

  8. LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)

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

  9. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  10. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

随机推荐

  1. Canvas学习

    参考了慕课网课程:炫丽的倒计时效果Canvas绘图与动画基础  感谢  liuyubobobo 老师 ,提供了这么好的课程 1.<canvas><canvas>标签     注 ...

  2. supervisor安装和配置

    直接命令 easy_install supervisor 如果报错先安装 yum install python-setuptools,再上面一条命令: 安装成功后显示finished,我们再次进行py ...

  3. 响应式 css

    1.class 样式一般用class,命名:中横线分隔,如:div-logo id 一般用于js快速地区别和获取元素,命名:驼峰命名法,如:divLogo (中间首字母大写) 2.必不可少的图片,用& ...

  4. iOS开发 ReactiveCocoa入门教程 第一部分

    作为一个iOS开发者,你写的每一行代码几乎都是在响应某个事件,例如按钮的点击,收到网络消息,属性的变化(通过KVO)或者用户位置的变化(通过CoreLocation).但是这些事件都用不同的方式来处理 ...

  5. Evolutionary Computing: 3. Genetic Algorithm(2)

    承接上一章,接着写Genetic Algorithm. 本章主要写排列表达(permutation representations) 开始先引一个具体的例子来进行表述 Outline 问题描述 排列表 ...

  6. 主成分分析(PCA)核心思想

    参考链接:http://pinkyjie.com/2011/02/24/covariance-pca/ PCA的本质其实就是对角化协方差矩阵. PCA就是将高维的数据通过线性变换投影到低维空间上去,但 ...

  7. English Literature

    The website links of English Literature,which I wanno recommend to U is based on following. 数据结构 - 知 ...

  8. sphinx 源码阅读之分词,压缩索引,倒排——单词对应的文档ID列表本质和lucene无异 也是外部排序再压缩 解压的时候需要全部扫描doc_ids列表偏移量相加获得最终的文档ID

    转自:http://github.tiankonguse.com/blog/2014/12/03/sphinx-token-inverted-sort.html 外部排序 现在我们的背景是有16个已经 ...

  9. Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树

    E. Memory and Casinos 题目连接: http://codeforces.com/contest/712/problem/E Description There are n casi ...

  10. IT公司100题-tencent-打印所有高度为2的路径

    问题描述: 打印所有到叶子节点长度为2的路径  10  /  \ 6   16 / \   / \ 4 8  14 18   / \    / \    \ 2  5  12 15 20 / 11   ...