-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path95.py
31 lines (29 loc) · 845 Bytes
/
95.py
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
"""
Definition of TreeNode:
"""
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
class Solution:
"""
@param root: The root of binary tree.
@return: True if the binary tree is BST, or false
"""
def isValidBST(self, root):
# write your code here
if root == None:
return True
res = []
res = Solution.midTraverse(root, root, res)
for i in range(len(res) - 1):
if res[i] >= res[i + 1]:
return False
return True
def midTraverse(self, root, res_arr):
if root == None:
return res_arr
res_arr = Solution.midTraverse(root, root.left, res_arr)
res_arr.append(root.val)
res_arr = Solution.midTraverse(root, root.right, res_arr)
return res_arr