题解 | #判断是不是二叉搜索树#
判断是不是二叉搜索树
https://www.nowcoder.com/practice/a69242b39baf45dea217815c7dedb52b
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return bool布尔型
*/
void inOrder(struct TreeNode* root , int* arr , int* returnSize)
{
if(root != NULL)
{
inOrder(root->left, arr,returnSize);
arr[(*returnSize)++] = root->val;
inOrder(root->right, arr,returnSize);
}
return;
}
bool isValidBST(struct TreeNode* root ) {
// write code here
int* arr = (int*)malloc(sizeof(int)*100000);
int returnSize = 0;
inOrder(root , arr , &returnSize);
for(int i = 0;i<returnSize-1;i++)
{
if(arr[i]>arr[i+1])
return false;
}
return true;
}
老板电器公司氛围 197人发布
查看19道真题和解析