FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode-Solutions/Python/validate-binary-search-tree.py at master · Monika-R/LeetCode-Solutions · GitHub
Monika-R
/
LeetCode-Solutions
Public
forked from
kamyu104/LeetCode-Solutions
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LeetCode-Solutions
/
Python
/
validate-binary-search-tree.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
56 lines (46 loc) · 1.48 KB
Breadcrumbs
LeetCode-Solutions
/
Python
/
validate-binary-search-tree.py
Copy path
File metadata and controls
56 lines (46 loc) · 1.48 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
# Time: O(n)
# Space: O(1)
class
TreeNode
(
object
):
def
__init__
(
self
,
x
):
self
.
val
=
x
self
.
left
=
None
self
.
right
=
None
# Morris Traversal Solution
class
Solution
(
object
):
# @param root, a tree node
# @return a list of integers
def
isValidBST
(
self
,
root
):
prev
,
cur
=
None
,
root
while
cur
:
if
cur
.
left
is
None
:
if
prev
and
prev
.
val
>=
cur
.
val
:
return
False
prev
=
cur
cur
=
cur
.
right
else
:
node
=
cur
.
left
while
node
.
right
and
node
.
right
!=
cur
:
node
=
node
.
right
if
node
.
right
is
None
:
node
.
right
=
cur
cur
=
cur
.
left
else
:
if
prev
and
prev
.
val
>=
cur
.
val
:
return
False
node
.
right
=
None
prev
=
cur
cur
=
cur
.
right
return
True
# Time: O(n)
# Space: O(h)
class
Solution2
(
object
):
# @param root, a tree node
# @return a boolean
def
isValidBST
(
self
,
root
):
return
self
.
isValidBSTRecu
(
root
,
float
(
"-inf"
),
float
(
"inf"
))
def
isValidBSTRecu
(
self
,
root
,
low
,
high
):
if
root
is
None
:
return
True
return
low
<
root
.
val
and
root
.
val
<
high
\
and
self
.
isValidBSTRecu
(
root
.
left
,
low
,
root
.
val
) \
and
self
.
isValidBSTRecu
(
root
.
right
,
root
.
val
,
high
)
Back
|
FazBrowse Home
|
New Git URL