判断平衡二叉树
原创
©著作权归作者所有:来自51CTO博客作者mb6300b232419d1的原创作品,请联系作者获取转载授权,否则将追究法律责任
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool Less(TreeNode *root, int data)
{
if(root == NULL)
return true;
return (root->val < data) && Less(root->left, data) && Less(root->right, data);
}
bool More(TreeNode *root, int data)
{
if(root == NULL)
return true;
return (root->val > data) && More(root->left, data) && More(root->right, data);
}
bool isValidBST(TreeNode *root) {
if(root == NULL)
return true;
return Less(root->left, root->val) && More(root->right, root->val) && isValidBST(root->left) && isValidBST(root->right);
}
};