Minimum Distance Between BST Nodes

🏠 ⬅️ ➑️

Given the root of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree.

Example 1:

Input: root = [4,2,6,1,3] Output: 1

Example 2:

Input: root = [1,0,48,null,null,12,49] Output: 1

Constraints:

  • The number of nodes in the tree is in the range [2, 100].
  • 0 <= Node.val <= 105

Note: This question is the same as 530: https://leetcode.com/problems/minimum-absolute-difference-in-bst/


Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    use, intrinsic :: iso_fortran_env, only : error_unit, DP => REAL64
    implicit none
    integer, parameter :: n = 2
    real(DP), dimension(n) :: x
    real(DP) :: y
    integer :: i

    ! Examples
    x(1) = 4._DP
    x(2) = 2._DP
    call test_case(x, 1._DP)

    x(1) = 1._DP
    x(2) = 48._DP
    call test_case(x, 1._DP)

contains

    subroutine test_case(x, expected)
        real(DP), intent(in) :: x(:)
        real(DP), intent(in) :: expected
        real(DP) :: result

        result = min_diff(x)
        write (unit=error_unit, fmt='(A, F10.3, A, F10.3, A)') &
            'Test case: ', x, ' -> ', result, ' (expected: ', expected, ')'
    end subroutine test_case

end program main

function min_diff(root) result(res)
    type(node), pointer, intent(in) :: root
    real(DP) :: res
    real(DP) :: diff

    if (.not. associated(root)) then
        res = -1._DP
        return
    end if

    diff = abs(root%val - root%left%val)
    res = min(res, diff)
    res = min(res, min_diff(root%left))
    res = min(res, min_diff(root%right))
end function min_diff

type node
    real(DP) :: val
    type(node), pointer :: left
    type(node), pointer :: right
end type node
❌ Compiled
❌ Executed
❌ Correct

🌐 Data from online sources
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def searchBST(root: TreeNode, val: int) -> TreeNode:
    if not root or root.val == val:
        return root
    return searchBST(root.left, val) if root.val > val else searchBST(root.right, val)

We are given a binary search tree (BST) and an integer value val. Our task is to find the node in the tree with the same value as val and return the subtree rooted at that node. If there is no such node, we should return null.

To solve this problem, we use a recursive function searchBST. We first check whether the current root is null or has the value we are searching for. If either condition is true, we return the root.

Otherwise, we compare the given val with the current node's value. If val is less than the current node's value, we proceed to search the left subtree. Otherwise, we search the right subtree, and we return the result of either search.

This algorithm leverages the properties of a binary search tree, which ensures that for each node, all elements in the left subtree are less than the node, and all elements in the right subtree are greater than the node, enabling efficient search.

🌐 Data from online sources
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

TreeNode* searchBST(TreeNode* root, int val) {
    if (!root || root->val == val) {
        return root;
    }
    return (root->val > val) ? searchBST(root->left, val) : searchBST(root->right, val);
}

We are given a binary search tree (BST) and an integer value val. Our task is to find the node in the tree with the same value as val and return the subtree rooted at that node. If there is no such node, we should return null.

To solve this problem, we use a recursive function searchBST. We first check whether the current root is null or has the value we are searching for. If either condition is true, we return the root.

Otherwise, we compare the given val with the current node's value. If val is less than the current node's value, we proceed to search the left subtree. Otherwise, we search the right subtree, and we return the result of either search.

This algorithm leverages the properties of a binary search tree, which ensures that for each node, all elements in the left subtree are less than the node, and all elements in the right subtree are greater than the node, enabling efficient search.