Leetcode 104 & 111. Minimum & Maximum Depth of Binary Tree
Maximum Depth of Binary Tree
Minimum Depth of Binary Tree
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
l=self.maxDepth(root.left)+1
r=self.maxDepth(root.right)+1
return max(l,r)
Minimum Depth of Binary Tree
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
d=[]
if not root:
return 0
def minD(root,num):
if not root:
return
if not root.left and not root.right:
d.append(num)
else:
minD(root.left,num+1)
minD(root.right,num+1)
minD(root,1)
return min(d)