Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
bool isValidBST(TreeNode *root) { //C++ if(root == NULL||(root->left==NULL &&root->right==NULL)) return true; int pre; bool isFirst = true; stack<TreeNode*> myStack; TreeNode* p = root; while(p!=NULL || !myStack.empty()) { while(p!=NULL){ myStack.push(p); p = p->left; } p = myStack.top(); if(isFirst){ isFirst = false; pre = p->val; } else{ if(p->val <=pre) return false; pre = p->val; } myStack.pop(); p = p->right; } return true; }
[leetcode] 98 Validate Binary Search Tree
原文地址:http://blog.csdn.net/chenlei0630/article/details/42718747