forked from algorhythms/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path098 Validate Binary Search Tree.py
68 lines (55 loc) · 1.83 KB
/
098 Validate Binary Search Tree.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
"""
__author__ = 'Danyang'
# Definition for a binary tree node
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def isValidBST(self, root):
"""
Google Phone Interview Question, 20 Sep 2013
recursive dfs
alternative answer: convert the tree the array and judge whether it is sorted
:param root: a tree node
:return: boolean
"""
if not root:
return True
if not self.isValidBST(root.left):
return False
if not self.isValidBST(root.right):
return False
if root.left:
if not self.get_largest(root.left) < root.val:
return False
if root.right:
if not root.val < self.get_smallest(root.right):
return False
return True
def get_largest(self, root):
"""
possible dp
:param root: TreeNode
:return: integer
"""
if not root.right:
return root.val
return self.get_largest(root.right)
def get_smallest(self, root):
"""
possible dp
:param root: TreeNode
:return: integer
"""
if not root.left:
return root.val
return self.get_smallest(root.left)