Skip to content

Commit fdfda1c

Browse files
committed
added first number greater than k
1 parent f06f188 commit fdfda1c

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Find the First Key Greater Than A Given Value In A BST
2+
3+
<br>
4+
5+
- Write a program that takes as input a BST and a value, and retums the first key that would appear in an inorder traversal which is greater than the input value.
6+
For example, when applied to the BST in the Figure below, you should return 29 for input 23.
7+
8+
### Example 1
9+
Input: tree, 23
10+
Output: 29
11+
12+
![Binary Search Tree](../../../assets/bst.png)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Best Case: Time O(n) | Space O(h)
2+
#
3+
# Worst Case: Time O(n) | Space O(n)
4+
class TreeNode:
5+
def __init__(self, val=None, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
def find_first_greater_than_than_k(tree, k):
11+
subtree, first_so_far = tree, None
12+
while subtree:
13+
if subtree.val > k:
14+
first_so_far, subtree = subtree, subtree.left
15+
else:
16+
subtree = subtree.right
17+
18+
return first_so_far
19+
20+
21+
22+
23+
bst = TreeNode(19)
24+
bst_1 = TreeNode(7)
25+
bst_2 = TreeNode(43)
26+
bst_3 = TreeNode(3)
27+
bst_4 = TreeNode(11)
28+
bst_5 = TreeNode(23)
29+
bst_6 = TreeNode(47)
30+
bst_7 = TreeNode(2)
31+
bst_8 = TreeNode(5)
32+
bst_9 = TreeNode(17)
33+
bst_10 = TreeNode(37)
34+
bst_11 = TreeNode(53)
35+
bst_12 = TreeNode(13)
36+
bst_13 = TreeNode(29)
37+
bst_14 = TreeNode(41)
38+
bst_15 = TreeNode(31)
39+
40+
bst.left, bst.right = bst_1, bst_2
41+
# Left Subtree
42+
bst_1.left, bst_1.right = bst_3, bst_4
43+
bst_3.left, bst_3.right = bst_7, bst_8
44+
bst_9.left, bst_4.right = bst_12, bst_9
45+
46+
# Right Subtree
47+
bst_2.left, bst_2.right = bst_5, bst_6
48+
bst_5.right = bst_10
49+
bst_10.left, bst_10.right = bst_13, bst_14
50+
bst_13.right = bst_15
51+
bst_6.right = bst_11
52+
53+
54+
print(find_first_greater_than_than_k(bst, 2).val) # 3
55+
print(find_first_greater_than_than_k(bst, 23).val) # 29
56+
print(find_first_greater_than_than_k(bst, 53)) # None

EPI_prep/src/binary_search_trees/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ With a BST you can iterate through elements in sorted order in time O(n) (regard
2626
In this section we will solve common binary search tree questions that will help you understand the data structure very well. This will in turn give you the ability to solve other binary search questions
2727

2828
* [Search Binary Tree](0_search_bst/search_bst.py)
29+
* [Check If Binary Search Tree Satisfies BST Property](1_check_if_bst_satisfies_bst_property/is_bst.py)
30+
* [Check If Binary Search Tree Satisfies BST Property - Solution 2](1_check_if_bst_satisfies_bst_property/is_bst_2.py)
31+
* [Find the First Key Greater Than A Given Value In A BST](2_find_first_key_greater_than_a_value_in_bst/find_first_greater_than_k.py)
32+
2933

patterns/src/2_two_pointers/dutch_national_flag/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ red, white and blue; and since our input array also consists of
88
three different numbers that is why it is called Dutch National Flag problem.
99

1010
### Example 1
11-
nput: [1, 0, 2, 1, 0]
11+
Input: [1, 0, 2, 1, 0]
1212
Output: [0, 0, 1, 1, 2]
1313

1414

0 commit comments

Comments
 (0)