leetcode第一刷_Validate Binary Search Tree
时间:2014-05-09 01:33:43
收藏:0
阅读:269
有了上面的教训,这道题就简单多了,什么时候该更新pre是明确的了,倒是有个细节,二叉搜索树中是不允许有相等节点的,所以题目的要求用黑体字标明了。写的时候注意就可以了。
class Solution { public: TreeNode *pre = NULL; bool isValidBST(TreeNode *root) { if(root == NULL) return true; bool res = true; if(root->left) res &= isValidBST(root->left); if(pre&&root->val<=pre->val){ return false; } pre = root; if(root->right) res &= isValidBST(root->right); return res; } };
评论(0)