Skip to content

Commit dec6f96

Browse files
authored
Create MinimumDepthLeverOrder.py
1 parent 4fff147 commit dec6f96

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2+
# E-Mail : [email protected]
3+
# Creation Date : 2014-01-10 06:15:46
4+
# Last modification : 2008-10-31
5+
# by : Narasimha Karumanchi
6+
# Book Title : Data Structures And Algorithmic Thinking With Python
7+
# Warranty : This software is provided "as is" without any
8+
# warranty; without even the implied warranty of
9+
# merchantability or fitness for a particular purpose.
10+
11+
# Definition for a binary tree node.
12+
class TreeNode:
13+
def __init__(self, x):
14+
self.val = x
15+
self.left = None
16+
self.right = None
17+
18+
class Solution:
19+
def minimumDepth(self, root):
20+
if root is None:
21+
return 0
22+
queue = []
23+
queue.append((root, 1))
24+
while queue:
25+
current, depth = queue.pop(0)
26+
if current.left is None and current.right is None:
27+
return depth
28+
if current.left:
29+
queue.append((current.left, depth+1))
30+
if current.right:
31+
queue.append((current.right, depth+1))
32+
33+
34+
tree = TreeNode(1)
35+
tree.left = TreeNode(2)
36+
tree.left.left = TreeNode(3)
37+
tree.left.right = TreeNode(4)
38+
# tree.right = TreeNode(2)
39+
# tree.right.right = TreeNode(3)
40+
# tree.right.left = TreeNode(4)
41+
42+
temp = Solution()
43+
print temp.minimumDepth(tree)

0 commit comments

Comments
 (0)