# 刷题day6

## 1 二叉树的最大深度

class Node(object):
def __init__(self,node):
self.elem =node
self.left = None
self.right = None
class solution(object):
def maxdeepth(self,root):
if root == None:
return 0
else:
return 1 + max(max(self.maxdeepth(root.right),self.maxdeepth(root.left))


## 2 验证二叉搜索树

• 节点的左子树只包含小于当前节点的数
• 节点的右子树只包含大于当前节点的数
• 所有的子树自身必须是二叉搜索树
class Treenode(object):
def __init__(self,item):
self.elem = item
self.left = None
self.right = None
class solution(object):
def isvalidbst(self,root):
import sys
min = sys.maxsize * (-1)
max = sys.maxsize
return self.validbst(root,min,max)
def validbst(self,min,max):
if root == None:
return True
if root.elem <= min or root.elem >= max:
return False
return self.validbst(root.left,min,root.val) and self.validbst(root.right,root.val,max)
# 左子树的数 小于 根节点 小于 右子树



## 3 将有序数组转换成二叉搜索树

class TreeNode(object):
def __init__(self,node):
self.elem = node
self.left = None
self.right = None
class solution(object):
def arraytobst(self,nums):
if not nums:
return None
mid = len(nums) // 2
root = TreeNode(nums[mid])
root.left = self.arraytobst(nums[:mid])
root.right = self.arraytobst(nums[mid+1:])
return root


08-13
09-08 38

10-09 47
10-21 72
10-25 70
10-06 21
11-02 12
05-05 55
10-09 25