Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
1
1-homework-yanjun
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bonnieyan
1-homework-yanjun
Commits
a0fb4544
Commit
a0fb4544
authored
6 years ago
by
bonnieyan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
提交作业
parent
cfc9a95d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
0 deletions
+35
-0
8-homework-yanjun/tree_depth.py
+35
-0
No files found.
8-homework-yanjun/tree_depth.py
0 → 100644
View file @
a0fb4544
class
node
():
def
__init__
(
self
,
k
=
None
,
l
=
None
,
r
=
None
):
self
.
val
=
k
self
.
left
=
l
self
.
right
=
r
def
listcreattree
(
root
,
llist
,
i
):
if
i
<
len
(
llist
):
if
llist
[
i
]
==
'null'
:
return
None
###这里的return很重要
else
:
root
=
node
(
k
=
llist
[
i
])
# print(llist[i])
root
.
left
=
listcreattree
(
root
.
left
,
llist
,
2
*
i
+
1
)
root
.
right
=
listcreattree
(
root
.
right
,
llist
,
2
*
i
+
2
)
return
root
###这里的return很重要
return
root
def
tree_depth
(
root
):
if
root
==
None
:
return
0
#最大深度
return
max
(
tree_depth
(
root
.
left
),
tree_depth
(
root
.
right
))
+
1
# return min(tree_depth(root.left), tree_depth(root.right)) + 1 最小深度
if
__name__
==
'__main__'
:
root
=
node
()
# llist = ['1', '2', '3', '#', '4', '5']
llist
=
[
3
,
9
,
20
,
'null'
,
'null'
,
15
,
7
]
root
=
listcreattree
(
root
,
llist
,
0
)
result
=
tree_depth
(
root
)
print
(
result
)
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment