平衡二叉树的验证,中序遍历的结果是否有序。
class Solution {
public:
void inorder(TreeNode* root,vector<int> &array){
if(root==NULL){
return ;
}
inorder(root->left,array);
array.push_back(root->val);
inorder(root->right,array);
}
bool isValidBST(TreeNode* root) {
if(root==NULL)
return true;
vector <int> vec;
inorder(root,vec);
for(int i=1;i<vec.size();i++){
if(vec[i]<=vec[i-1])
return false;
}
return true;
}
};