//判断二叉树是否是平衡搜索树
//中序遍历判断即可
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Checker { public: bool checkBST(TreeNode* root) { stack<TreeNode*> s; TreeNode *pNode = root; vector<int> data; while (pNode != NULL || !s.empty()) { while (pNode != NULL) { s.push(pNode); pNode = pNode->left; } if (!s.empty()) { pNode = s.top(); data.push_back(pNode->val); s.pop(); pNode = pNode->right; } } for (size_t i = 0; i < data.size()-1; i++) { if (data[i] > data[i + 1]) return false; } return true; } };
时间: 2024-10-24 03:44:36