[LeetCode Daily Question] - 783. The minimum distance between nodes in a binary search tree

One [topic category]

  • depth-first search

Two [question difficulty]

  • Simple

Three [topic number]

  • 783. Minimum Distance Between Binary Search Tree Nodes

Four [title description]

  • Given the root node root of a binary search tree, return the minimum difference between any two different node values ​​in the tree.
  • Difference is a positive number whose value is equal to the absolute value of the difference between the two values.

Five [topic examples]

  • Example 1:

    • insert image description here
    • Input: root = [4,2,6,1,3]
    • Output: 1
  • Example 2:

    • insert image description here
    • Input: root = [1,0,48,null,null,12,49]
    • Output: 1

Six [problem-solving ideas]

  • This question is relatively simple, using the nature of the binary search tree: in-order traversal of the binary search tree will result in an ascending sequence
  • Since it is an ascending sequence, the minimum difference must appear among adjacent values, so you only need to use pre to record the predecessor node of the current node root, record the difference for comparison, and update if there is a smaller difference
  • Finally return the result

Seven [title prompt]

  • The range of the number of nodes in the tree is [ 2 , 100 ] The range of the number of nodes in the tree is [2, 100]The range of the number of nodes in the tree is [ 2 ,100]
  • 0 < = N o d e . v a l < = 1 0 5 0 <= Node.val <= 10^5 0<=Node.val<=105

Eight [topic note]

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

Nine [time frequency]

  • Time complexity: O ( n ) O(n)O ( n ) , wherennn is the number of nodes passed in the binary tree
  • Space Complexity: O ( n ) O(n)O ( n ) , wherennn is the number of nodes passed in the binary tree

Ten [code implementation]

  1. Java language version
class Solution {
    
    

    TreeNode pre = null;
    int min = Integer.MAX_VALUE;

    public int minDiffInBST(TreeNode root) {
    
    
        inOrder(root);
        return min;
    }

    public void inOrder(TreeNode root){
    
    
        if(root != null){
    
    
            inOrder(root.left);
            if(pre != null){
    
    
                min = Math.min(min,Math.abs(pre.val - root.val));
            }
            pre = root;
            inOrder(root.right);
        }
    }

}
  1. C language version
void inOrder(struct TreeNode* root,int* pre,int* min)
{
    
    
    if(root != NULL)
    {
    
    
        inOrder(root->left,pre,min);
        if(*pre != -1)
        {
    
    
            *min = fmin(*min,abs(root->val - (*pre)));
        }
        *pre = root->val;
        inOrder(root->right,pre,min);
    }
}

int minDiffInBST(struct TreeNode* root)
{
    
    
    int* pre = -1;
    int min = INT_MAX;
    inOrder(root,&pre,&min);
    return min;
}
  1. Python language version
class Solution:
    def minDiffInBST(self, root: Optional[TreeNode]) -> int:
        def inOrder(root):
            nonlocal res,pre
            if root != None:
                inOrder(root.left)
                if pre != -1:
                    res = min(res,abs(root.val - pre))
                pre = root.val
                inOrder(root.right)
        pre = -1
        res = float('inf')
        inOrder(root)
        return res
  1. C++ language version
class Solution {
    
    
public:
    void inOrder(TreeNode* root,int& pre,int& res)
    {
    
    
        if(root != nullptr)
        {
    
    
            inOrder(root->left,pre,res);
            if(pre != -1)
            {
    
    
                res = min(res,abs(root->val - pre));
            }
            pre = root->val;
            inOrder(root->right,pre,res);
        }
    }

    int minDiffInBST(TreeNode* root) 
    {
    
    
        int pre = -1;
        int res = INT_MAX;
        inOrder(root,pre,res);
        return res;
    }
};

Eleven [submission results]

  1. Java language version
    insert image description here

  2. C language version
    insert image description here

  3. Python language version
    insert image description here

  4. C++ language version
    insert image description here

Guess you like

Origin blog.csdn.net/IronmanJay/article/details/129435079