783. Minimum Distance of the Binary Search Tree node

Source: Internet
Author: User

Given the root node of a binary search treerootReturns the minimum deviation of any two vertices in the tree.

Example:

Input:Root = [4, 2, 6, 1, 3, null, null]Output:1Explanation:Note that root is a tree Node object instead of an array. The given tree [4/2, 3, null, null] can be expressed as: 6/\ 1 3. The smallest difference is 1, which is the difference between node 1 and node 2, it is also the difference between node 3 and node 2.

Note:

  1. The size range of the binary tree is2To100.
  2. Binary Trees are always valid. The values of each node are integers and are not repeated.
        int minDiffInBST(TreeNode* root) {    vector<int > res;    inorder(root, res);    int Min = 65535;    for (int i = 0; i<res.size() - 1; i++){        Min = min(Min, res[i + 1] - res[i]);    }    return Min;}TreeNode * inorder(TreeNode* root, vector<int >&res){    if (root==NULL)        return NULL;    inorder(root->left, res);    res.push_back(root->val);    inorder(root->right, res);    return root;}

     

783. Minimum Distance of the Binary Search Tree node

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.