您的位置:首页 > 其它

LeetCode110. Balanced Binary Tree

2016-12-30 11:08 447 查看
题目链接

判断是否为平衡二叉树。

class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root == NULL) return 1;
int l1 = getDepth(root->left);
int l2 = getDepth(root->right);
if(abs(l1-l2)>1) return 0;
return isBalanced(root->left)&&isBalanced(root->right);

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