FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms/tree/bst/array2bst.py at master · VirtualCycle/algorithms · GitHub
VirtualCycle
/
algorithms
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
/
tree
/
bst
/
array2bst.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
19 lines (17 loc) · 444 Bytes
Breadcrumbs
algorithms
/
tree
/
bst
/
array2bst.py
Copy path
File metadata and controls
19 lines (17 loc) · 444 Bytes
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
"""
Given an array where elements are sorted in ascending order,
convert it to a height balanced BST.
"""
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
def
array2bst
(
nums
):
if
not
nums
:
return
None
mid
=
len
(
nums
)
//
2
node
=
Node
(
nums
[
mid
])
node
.
left
=
array2bst
(
nums
[:
mid
])
node
.
right
=
array2bst
(
nums
[
mid
+
1
:])
return
node
Back
|
FazBrowse Home
|
New Git URL