File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2
+
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 )
You can’t perform that action at this time.
0 commit comments