您的位置:首页 > 其它

110 Balanced Binary Tree

2015-11-14 20:32 357 查看
/**
* 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:
bool isBalanced(TreeNode* root) {
if(root==NULL)
return true;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
if(abs(left-right) > 1)
return false;
return isBalanced(root->left)&&isBalanced(root->right);

}
int maxDepth(TreeNode* root) {

if(root==NULL)
return 0;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return max(left,right)+1;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: