繁体   English   中英

如何获得 javascript 中两个值或节点之间的差异

[英]how to get difference between two values or nodes in javascript

我正在尝试解决这个问题。我没有得到预期 output

给定具有根节点 root 的二叉搜索树 (BST),返回树中任意两个不同节点的值之间的最小差值。

例子:

输入:

root = [4,2,6,1,3,null,null]

Output:

1

解释:

请注意,root 是 TreeNode object,而不是数组。

给定的树 [4,2,6,1,3,null,null] 由下图表示:

      4
    /   \
  2      6
 / \    
1   3  

虽然这棵树的最小差异为 1,但它发生在节点 1 和节点 2 之间,也发生在节点 3 和节点 2 之间。

我试过这样

var minDiffInBST = function (root) {
    let min = Number.MAX_VALUE
    const getMin = (node) => {
        if (node.left && node.right) {
            console.log('both')
            return Math.min(node.val - node.left.val, node.right.val - node.val)
        } else if (node.right) {
            console.log('right')
            return node.right.val - node.val
        } else if (node.left) {
            console.log('left')
            return node.val - node.left.val
        } else {
            return Number.MAX_VALUE
        }
    }

    const preOrder = (root) => {
        if (!root) {
            return 0;
        }
        let x = getMin(root)
        if (x < min)
            min = x
        preOrder(root.left)
        preOrder(root.right)

    }
    preOrder(root)
    return min
};

console.log(minDiffInBST({
        "val": 90,
        "left": {
            "val": 69,
            "left": {"val": 49, "left": null, "right": {"val": 52, "left": null, "right": null}},
            "right": {"val": 89, "left": null, "right": null}
        },
        "right": null
    }

))

得到 output 3预期 output 1

问题我来自这里https://leetcode.com/problems/minimum-distance-between-bst-nodes/

除了 Paul Nikonowicz 评论,这里是我在 C++ 中的实现,使用中序遍历来获得 BST 的排序向量,然后对其进行迭代,将每个结果值与向量的最大值进行比较

#include<iostream> 
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};


class Solution {
public:
    vector<int> toCompare = {};

    /* find min diff pair of our inorder derived vector*/
    int find_min(vector <int> const& a) {
        int diff = *std::max_element(a.begin(), a.end());
        for (int i = 0; i < a.size()-1; i++)
            if (a.at(i + 1) - a.at(i) < diff) {
                diff = a.at(i + 1) - a.at(i);

            }
        return diff;
    }

    int minDiffInBST(TreeNode* root) {
   
        if (root == NULL) {
            return 0;
        };

        /* In order traversal */
        minDiffInBST(root->left);
        /* Storing in vector/list */
        toCompare.push_back(root->val);

        minDiffInBST(root->right);
 
        return find_min(toCompare);    
      
    };
};


int main() {
    struct TreeNode* root = new TreeNode(5);
    root->left = new TreeNode(4);
    root->right = new TreeNode(6);
    root->left->left = new TreeNode(3);
    
    Solution myObj;

    cout << myObj.minDiffInBST(root);
    return 0;

您需要执行顺序遍历,而不是前序遍历。

在 BST 中,中序遍历为您提供了一个排序列表。 在排序列表中,两个元素之间的最小距离将彼此最接近。

所以,首先解决一个排序列表,然后解决一个 BST 遍历的IN ORDER

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM