Minimum Distance Between BST Nodes Solutions in C++
Number 783
Difficulty Easy
Acceptance 52.7%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/minimum-distance-between-bst-nodes/description//// Author : liuyubobobo/// Time : 2010-02-10#include <iostream>#include <cassert>using namespace std;/// Brute Force/// For every node, find his prev and next node and compare the diff////// Time Complexity: O(n^2)/// Space Complexity: O(logn)/// 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) {if(root == NULL)return INT_MAX;if(root->left == NULL && root->right == NULL)return INT_MAX;int diff1 = root->left ? root->val - maxInBST(root->left) : INT_MAX;int diff2 = root->right ? minInBST(root->right) - root->val : INT_MAX;return min(min(diff1, diff2),min(minDiffInBST(root->left), minDiffInBST(root->right)));}private:int maxInBST(TreeNode* node){assert(node != NULL);if(node->right)return maxInBST(node->right);return node->val;}int minInBST(TreeNode* node){assert(node != NULL);if(node->left)return minInBST(node->left);return node->val;}};int main() {// [90,69,null,49,89,null,52,null,null,null,null]return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/minimum-distance-between-bst-nodes/description//// Author : liuyubobobo/// Time : 2010-02-11#include <iostream>#include <vector>#include <algorithm>#include <cassert>using namespace std;/// Using Array/// Store All Numbers in an array and sort, then get the min distance////// Time Complexity: O(nlogn)/// Space Complexity: O(n)/// 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) {vector<int> nums;dfs(root, nums);sort(nums.begin(), nums.end());int res = INT_MAX;for(int i = 1 ; i < nums.size() ; i ++)res = min(res, nums[i] - nums[i-1]);return res;}private:void dfs(TreeNode* node, vector<int>& nums){if(node == NULL)return;nums.push_back(node->val);dfs(node->left, nums);dfs(node->right, nums);}};int main() {// [90,69,null,49,89,null,52,null,null,null,null]return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/minimum-distance-between-bst-nodes/description//// Author : liuyubobobo/// Time : 2010-02-11#include <iostream>#include <vector>#include <algorithm>#include <cassert>using namespace std;/// Using Array/// Inorder traverse the tree and put every number in an array./// No need to sort the array.////// Time Complexity: O(n)/// Space Complexity: O(n)/// 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) {vector<int> nums;dfs(root, nums);int res = INT_MAX;for(int i = 1 ; i < nums.size() ; i ++)res = min(res, nums[i] - nums[i-1]);return res;}private:void dfs(TreeNode* node, vector<int>& nums){if(node == NULL)return;dfs(node->left, nums);nums.push_back(node->val);dfs(node->right, nums);}};int main() {// [90,69,null,49,89,null,52,null,null,null,null]return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/minimum-distance-between-bst-nodes/description//// Author : liuyubobobo/// Time : 2010-02-11#include <iostream>#include <vector>#include <algorithm>#include <cassert>using namespace std;/// Inorder Traverse/// Inorder traverse the tree and record every neighbor numbers diff during the process.////// Time Complexity: O(n)/// Space Complexity: O(logn)/// 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 {private:int* lastNumber;public:int minDiffInBST(TreeNode* root) {int res = INT_MAX;lastNumber = NULL;dfs(root, res);return res;}private:void dfs(TreeNode* node, int& res){if(node == NULL)return;dfs(node->left, res);if(lastNumber != NULL){// cout << node->val << " " << (*lastNumber) << endl;res = min(res, node->val - *lastNumber);}lastNumber = &(node->val);dfs(node->right, res);}};int main() {// [90,69,null,49,89,null,52,null,null,null,null]// [27,null,34,null,58,50,null,44,null,null,null]TreeNode* node44 = new TreeNode(44);TreeNode* node50 = new TreeNode(50);TreeNode* node58 = new TreeNode(58);TreeNode* node34 = new TreeNode(34);TreeNode* node27 = new TreeNode(27);node27->right = node34;node34->right = node58;node58->left = node50;node50->left = node44;cout << Solution().minDiffInBST(node27);return 0;}