1、二叉搜索樹:左邊大於中間大於右邊
前序遍歷的遞歸代碼:
if(root==nullptr) return;
bfs(root->left);
res.push_back(root->val);
bfs()root->right);
/**
* 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:
vector<int>vec;
void bfs(TreeNode* root)
{
if(root==nullptr)return;
bfs(root->left);
vec.push_back(root->val);
bfs(root->right);
}
int minDiffInBST(TreeNode* root) {
int mindiffer=INT_MAX;
int diff=0;
bfs(root);
for(int i=0;i<vec.size()-1;i++)
{
diff=vec[i+1]-vec[i];
if(diff<mindiffer) mindiffer=diff;
}
return mindiffer;
}
};