We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5281a74 commit df54080Copy full SHA for df54080
1 file changed
validate-binary-search-tree/hyeri0903.py
@@ -0,0 +1,35 @@
1
+# Definition for a binary tree node.
2
+# class TreeNode:
3
+# def __init__(self, val=0, left=None, right=None):
4
+# self.val = val
5
+# self.left = left
6
+# self.right = right
7
+class Solution:
8
+ def isValidBST(self, root: Optional[TreeNode]) -> bool:
9
+ """
10
+ checking validation using inorder traversal
11
+ always greater than previous node's key
12
+
13
+ time complexity: O(n)
14
15
16
17
+ prev = [None]
18
19
+ def inorder(node):
20
+ if not node:
21
+ return True
22
23
+ #search left sub tree first
24
+ if not inorder(node.left):
25
+ return False
26
27
+ if prev[0] is not None and node.val <= prev[0]:
28
29
30
+ #set current node value
31
+ prev[0] = node.val
32
+ #search right sub tree
33
+ return inorder(node.right)
34
35
+ return inorder(root)
0 commit comments