110. Balanced Binary Tree(1)

来源:互联网 发布:怎么发淘宝优惠券赚钱 编辑:程序博客网 时间:2024/06/03 11:19
/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    int getheight(TreeNode* root) {        if(!root) return 0;        if(!root->left&&!root->right) return 1;        return max(getheight(root->left),getheight(root->right))+1;    }    bool isBalanced(TreeNode* root) {        if(!root) return true;        if(!root->left&&!root->right) return true;        int l=getheight(root->left);        int r=getheight(root->right);        if(abs(l-r)>1) return false;        if(isBalanced(root->left)&&isBalanced(root->right)) return true;        return false;    }};
0 0
原创粉丝点击