LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
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]的更多相关文章
- LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- 【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 ...
- 【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 ...
- [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 ...
- 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 ...
- [LeetCode]题解(python):104 Maximum Depth of Binary Tree
题目来源 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Given a binary tree, find its maxim ...
- 【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree
今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的.由于c++版的代码网上比較多.所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做Lee ...
- 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 ...
- 【一天一道LeetCode】#104. Maximum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
随机推荐
- Shell终端配置
Shell终端配置 How to: Change / Setup bash custom prompt (PS1) 参考链接:https://www.cyberciti.biz/tips/howto- ...
- 用spring的InitializingBean作初始化
org.springframework.beans.factory包下有一个接口是InitializingBean 只有一个方法: /** * Invoked by a BeanFactory af ...
- 编程之美----NIM游戏
: 博弈游戏·Nim游戏 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 今天我们要认识一对新朋友,Alice与Bob.Alice与Bob总是在进行各种各样的比试,今天他 ...
- mousedown(function(){ return false; })作用
mousedown(function(){ return false;}); 阻止浏览器的默认行为. 比如a你加个空连接,可能会在当前页跳转, 你加了这句,就可以阻止a跳转,然后只执行js函数的代 ...
- Project中分清楚挣值项
在项目管理非常重要的挣值管理,有一些关键项,像PV,EV,AC,BAC,EAC,ETC等等这些都是关键项,如果这个没分清楚,计算出很多东西都是错的,下面两个图是我一个项目快要完成的报表. 图1 图2 ...
- 创建.htaccess文件
在linux下创建.htaccess文件非常简单,直接新建一个文件并重命名为.htaccess即可. 下面我来讲下如何在Window下创建.htaccess文件 一般在本地电脑上是无法建立 .htac ...
- Jenkins+Jmeter+Ant 接口持续集成(转)
来源:https://testerhome.com/topics/5186 为什么要用Jmeter做接口测试 当选择这套方案的时候,很多人会问,为什么选择Jmeter做Case管理?为什么不自己写框架 ...
- Android - 广播接收者 - BroadcastReceiver
BroadcastReceiver 介绍: 广播是一种广泛运用的在应用程序之间传输信息的机制 .而 BroadcastReceiver 是对发送出来的广播 进行过滤接收并响应的一类组件 接受一种或者多 ...
- hdu 1041 (OO approach, private constructor to prevent instantiation, sprintf) 分类: hdoj 2015-06-17 15:57 25人阅读 评论(0) 收藏
a problem where OO seems more natural to me, implementing a utility class not instantiable. how to p ...
- Android中土司(Toast)的使用
Android中Toast的使用 什么是土司(Toast)? Toast是Android系统提供的一种非常好的提示方式,在程序中可以使用它将一些短小的信息通知给用户,这些信息会在一段时间后自动消失, ...