-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin-depth-bt.py
More file actions
27 lines (19 loc) · 781 Bytes
/
min-depth-bt.py
File metadata and controls
27 lines (19 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
#Base Case => H=1
if root.left is None and root.right is None:
return 1
if root.left is None:
#if left is none, go right
return self.minDepth(root.right)+1 #1 is height of tree
if root.right is None:
return self.minDepth(root.left)+1
return min(self.minDepth(root.left),self.minDepth(root.right))+1