BM34 判断是不是二叉搜索树
判断是不是二叉搜索树
https://www.nowcoder.com/practice/a69242b39baf45dea217815c7dedb52b?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj
题目虽然简单,但是也要把答案写的这么简单才好。
确定节点的左右边界,这个思路我没看到有人这么做,可以作为这类题的一个通用的思路。
bool isValidBST(TreeNode* root) { // write code here return isValidBSTTree(root,INT_MIN,INT_MAX); } bool isValidBSTTree(TreeNode* root,int leftVal,int rightVal) { if(root == NULL){ return true; } if(root->val < leftVal || root->val > rightVal){ return false; } return isValidBSTTree(root->left,leftVal,root->val) && isValidBSTTree(root->right,root->val,rightVal); }
来吧,BAT 文章被收录于专栏
来吧,BAT