一开始没看懂题==,原来是求树中各元素最小距离,排序=》中序遍历
/**
* 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 res = 99999999 ;
int pre = -1;
int minDiffInBST(TreeNode* root) {
if( root->left != NULL )minDiffInBST( root->left );
if( pre >= 0 )res = min( res , root->val - pre );
pre = root->val;
if( root->right != NULL )minDiffInBST( root->right );
return res;
}
};