• 783. Minimum Distance Between BST Nodes


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

    Example :

    Input: root = [4,2,6,1,3,null,null]
    Output: 1
    Explanation:
    Note that root is a TreeNode object, not an array.
    
    The given tree [4,2,6,1,3,null,null] is represented by the following diagram:
    
              4
            /   
          2      6
         /     
        1   3  
    
    while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.
    

    Note:

    1. The size of the BST will be between 2 and 100.
    2. The BST is always valid, each node's value is an integer, and each node's value is different.

    Approach #1: C++.

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int minDiffInBST(TreeNode* root) {
            solve(root);
            
            sort(nodes.begin(), nodes.end());
            
            int minNum = INT_MAX;
            
            for (int i = 1; i < nodes.size(); ++i) {
                minNum = min(minNum, nodes[i]-nodes[i-1]);
            }
            
            return minNum;
        }
        
        void solve(TreeNode* root) {
            if (root->left != NULL)
                minDiffInBST(root->left);
            
            nodes.push_back(root->val);
            
            if (root->right != NULL)
                minDiffInBST(root->right);
        }
        
        
    private:
        vector<int> nodes;
    };
    

      

    Approach #2: Java. [Inorder travel]

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        Integer minDiff = Integer.MAX_VALUE, pre = null;
        public int minDiffInBST(TreeNode root) {
            if (root.left != null) minDiffInBST(root.left);
            if (pre != null) minDiff = Math.min(minDiff, root.val-pre);
            pre = root.val;
            if (root.right != null) minDiffInBST(root.right);
            return minDiff;
        }
    }
    

      

    Approach #3: Python.

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        minDiff = float('inf')
        pre = -float('inf')
        
        def minDiffInBST(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if root.left != None:
                self.minDiffInBST(root.left)
            self.minDiff = min(self.minDiff, root.val-self.pre)
            self.pre = root.val
            if root.right != None:
                self.minDiffInBST(root.right)
            
            return self.minDiff;
            
    

      

    Notes:

    The Inf and NaN in python.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    《python深度学习》笔记---5.4-3、卷积网络可视化-热力图
    《python深度学习》笔记---5.4-2、卷积网络可视化-过滤器
    HTTPModule Event Execution Order?
    HttpModule的认识与深入理解
    ASP.NET底层与各个组件的初步认识与理解 汇总
    HttpRuntime的认识与加深理解
    AllowAnonymousAttribute Class
    AuthenticateRequest event
    FormsAuthentication.Decrypt(String) Method 在.net内部的调用
    Share cookie between subdomain and domain
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10063314.html
Copyright © 2020-2023  润新知