判断平衡二叉树

《判断平衡二叉树》

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

 

    原文作者:jihite
    原文地址: https://www.cnblogs.com/kaituorensheng/p/3653146.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞