diff --git a/530. Minimum Absolute Difference in BST.cpp b/530. Minimum Absolute Difference in BST.cpp index fc63556..8692739 100644 --- a/530. Minimum Absolute Difference in BST.cpp +++ b/530. Minimum Absolute Difference in BST.cpp @@ -11,28 +11,22 @@ */ class Solution { public: - vector data; + int ans = INT_MAX; - void inOrder(TreeNode* root) { + void inOrder(TreeNode* root, TreeNode* &previous) { if (root == NULL) return; - - inOrder(root->left); - data.push_back(root->val); - inOrder(root->right); + inOrder(root->left, previous); + if (previous != NULL) + ans = min(ans, root->val - previous->val); + previous = root; + inOrder(root->right, previous); } int getMinimumDifference(TreeNode* root) { - - inOrder(root); - int ans = INT_MAX; - for (int i=0; i