Comment on page

# 530.Minimum Absolute Difference in BST

530.Minimum Absolute Difference in BST

1
\
3
/
2

1

/**
* 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 getMinimumDifference(TreeNode* root) {
int m=std::numeric_limits<int>::max();
if(!root) return m;
int ld=m,rd=m;
if(root->left)
{
TreeNode* leftNode=root->left;
while(leftNode)
{
ld=root->val - leftNode->val;
leftNode=leftNode->right;
}
}
if(root->right)
{
TreeNode* rightNode=root->right;
while(rightNode)
{
rd=rightNode->val-root->val;
rightNode=rightNode->left;
}
}
// cout<<ld << " " <<rd << " "<< m<<endl;
if(root->left&& root->right)
{
m=min(ld,rd);
m=min(m,min(getMinimumDifference(root->left),getMinimumDifference(root->right)));
}
else if (root->left)
m=min(ld,getMinimumDifference(root->left));
else if(root->right) m=min(rd,getMinimumDifference(root->right));
else
return m;
// cout<<m<<endl;
return m;
}
};