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
 

最新文章

  1. FineReport:任意时刻只允许在一个客户端登陆账号的插件
  2. http与https的区别
  3. 正则表达式30分钟入门:http://deerchao.net/tutorials/regex/regex.htm#mission
  4. Matlab最短路径问题记录
  5. Linux中iptables设置详细(转)
  6. oracle ORA-00001:违反唯一约束条件
  7. [读行者][学习LinqExpression和Reflection(Emit)]阅读TypeBuilderSample之ExampleFromTheArticle
  8. border粗细不一
  9. 云服务和虚拟机的预留 IP 地址
  10. C# 读取Excel日期格式
  11. oracle数据库连接无响应的解决
  12. Python内置函数str()和repr()
  13. Eclipse中GIT插件更新工程到之前版本
  14. 用Zephir编写PHP扩展
  15. 让数字变化炫酷起来,数字滚动Text组件[Unity]
  16. C#中dll调用方法
  17. Vue笔记:生命周期和钩子函数
  18. xunit-ICollectionFixture
  19. buildroot构建项目(三)--- u-boot 2017.11 适配开发板修改 1
  20. PAT甲级1139 First Contact

热门文章

  1. 关于NOR-FLASH和NAND-fLASH的区别。——Arvin
  2. VIM 代码折叠
  3. freemarker if elseif
  4. 分治法求2n个数的中位数
  5. 给一个div绝对定位后,如何让它水平居中
  6. FBI是如何破获“美国少女”裸照勒索案的
  7. HDU 1561 树形DP入门
  8. Solr整合中文分词组件IKAnalyzer
  9. Java学习指南学习笔记
  10. 在Eclipse上建立hadoop2.2.0/hadoop2.4.0源代码阅读环境