FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms-python/algorithms/tree/min_height.py at master · ivan1911/algorithms-python · GitHub
ivan1911
/
algorithms-python
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms-python
/
algorithms
/
tree
/
min_height.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
54 lines (45 loc) · 1.29 KB
Breadcrumbs
algorithms-python
/
algorithms
/
tree
/
min_height.py
Copy path
File metadata and controls
54 lines (45 loc) · 1.29 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from
tree
import
TreeNode
def
min_depth
(
self
,
root
):
"""
:type root: TreeNode
:rtype: int
"""
if
root
is
None
:
return
0
if
root
.
left
is
not
None
or
root
.
right
is
not
None
:
return
max
(
self
.
minDepth
(
root
.
left
),
self
.
minDepth
(
root
.
right
))
+
1
return
min
(
self
.
minDepth
(
root
.
left
),
self
.
minDepth
(
root
.
right
))
+
1
# iterative
def
min_height
(
root
):
if
root
is
None
:
return
0
height
=
0
level
=
[
root
]
while
level
:
height
+=
1
new_level
=
[]
for
node
in
level
:
if
node
.
left
is
None
and
node
.
right
is
None
:
return
height
if
node
.
left
is
not
None
:
new_level
.
append
(
node
.
left
)
if
node
.
right
is
not
None
:
new_level
.
append
(
node
.
right
)
level
=
new_level
return
height
def
print_tree
(
root
):
if
root
is
not
None
:
print
(
root
.
val
)
print_tree
(
root
.
left
)
print_tree
(
root
.
right
)
if
__name__
==
'__main__'
:
tree
=
TreeNode
(
10
)
tree
.
left
=
TreeNode
(
12
)
tree
.
right
=
TreeNode
(
15
)
tree
.
left
.
left
=
TreeNode
(
25
)
tree
.
left
.
left
.
right
=
TreeNode
(
100
)
tree
.
left
.
right
=
TreeNode
(
30
)
tree
.
right
.
left
=
TreeNode
(
36
)
height
=
min_height
(
tree
)
print_tree
(
tree
)
print
(
"height:"
,
height
)
Back
|
FazBrowse Home
|
New Git URL