1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class { public: bool isValidBST(TreeNode* root) { if (root == nullptr) { return true; } stack<TreeNode*> stackNode; TreeNode* prev = nullptr; while (root != nullptr || !stackNode.empty()) { while (root != nullptr) { stackNode.push(root); root = root->left; } root = stackNode.top(); stackNode.pop(); if (prev != nullptr && root->val <= prev->val) { return false; } prev = root; root = root->right; } return true; } };
|
近期评论