Given a binary search tree, determine the minimum difference between any two different values in it. You may assume that the BST has at least two nodes.
Implement the function bstree_difference
in bstree_difference.c
. This
function will then be called by the main
function in bstree_difference.c
.
To run your solution:
$ make
$ ./test_bstree_difference
Upon running ./test_bstree_difference
, the user will be prompted to provide a
single line to standard input. This line should contain a space-separated list
of integer values, representing the preorder traversal of the BST.
The program will write to stdout. It will print Minimum difference:
followed
by the return value of the call made to bstree_difference
.
Input:
11 7 4 20 18 23 30
Output:
Minimum difference: 2
Explanation:
The BST is
11
/ \
/ \
7 20
/ / \
4 / \
18 23
\
30
and the minimum difference between any two different values in it is 20 - 18 = 2.
MAX_BST_SIZE
values.int
in C.MAX_LINE_LEN - 1
.When you think your program is working, you can use CSE autotest to test your solution.
$ 2521 autotest bstree_difference
Any efficient solution, such as one that runs in O(n) or O(n log n), should pass all of the autotests, but here are some extra challenges if you're interested:
Additional space refers to any space allocated between the time that the
function bstree_difference
is called and the time it returns.
You can view the solution code to this problem here.