判断平衡二叉树

/**
 * 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);
    }
};

 

posted @ 2014-04-08 22:45  jihite  阅读(581)  评论(0编辑  收藏  举报