Skip to content

Commit 8e68fef

Browse files
committed
230 Kth Smallest Element in a BST.py
98 Validate Binary Search Tree.py
1 parent 94e3301 commit 8e68fef

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

230 Kth Smallest Element in a BST.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Definition for a binary tree node.
2+
class TreeNode(object):
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
class Solution(object):
9+
def kthSmallest(self, root, k):
10+
"""
11+
:type root: TreeNode
12+
:type k: int
13+
:rtype: int
14+
"""
15+
inoerder_list = []
16+
def inoiterate(root):
17+
if not root:
18+
return None
19+
inoiterate(root.left)
20+
inoerder_list.append(root.val)
21+
inoiterate(root.right)
22+
23+
inoiterate(root)
24+
25+
return inoerder_list[k-1]

98 Validate Binary Search Tree.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for a binary tree node.
2+
class TreeNode(object):
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
class Solution(object):
9+
def isValidBST(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: bool
13+
"""
14+
if not root:
15+
return True
16+
test_left = True
17+
test_right = True
18+
if root.left:
19+
if root.left.val >= root.val:
20+
test_left = False
21+
if root.right:
22+
if root.right.val <= root.val:
23+
test_right = False
24+
return self.isValidBST(root.left) and self.isValidBST(root.right) and test_left and test_right

0 commit comments

Comments
 (0)