题解 | #判断是不是二叉搜索树#
判断是不是二叉搜索树
https://www.nowcoder.com/practice/a69242b39baf45dea217815c7dedb52b
C语言
//参考思路:把二叉树按照前序遍历把节点值压入栈,根据栈是否单调递增判断
void recursion(struct TreeNode* node, int *stack, int *st_i){
if(node == NULL)return;
recursion(node->left, stack, st_i); //前序遍历,把节点值压入栈
stack[(*st_i)++] = node->val;
recursion(node->right, stack, st_i);
}
bool isValidBST(struct TreeNode* root ) {
// write code here
int stack[10000] = {0};
int st_i = 0;
recursion(root, stack, &st_i);
for(int i=0; i<st_i-1; i++){ //判断栈是否单调递增
if(stack[i] >= stack[i+1])
return false;
}
return true;
}