Skip to content

Commit 872114d

Browse files
Min_Depth_of_Binary_Tree
1 parent c07e4c5 commit 872114d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def minDepth(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
if None == root:
15+
return 0
16+
if None == root.left:
17+
return self.minDepth(root.right) + 1
18+
if None == root.right:
19+
return self.minDepth(root.left) + 1
20+
else:
21+
return 1 + min(map(self.minDepth, (root.left, root.right)))
22+
23+
'''
24+
if root is Null, return 0
25+
if the left node is Null, return min_depth of the right node and add 1
26+
if the right node is Null, return min_depth of the left node and add 1
27+
otherwise, return the min_depth between left_node and right_node and add 1
28+
'''

0 commit comments

Comments
 (0)