题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
解:
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot == NULL)
return true;
if(abs(heightOfTree(pRoot->left) -heightOfTree(pRoot->right)) <= 1)
return IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
else
return false;
}
int heightOfTree(TreeNode* pRoot){
if(pRoot == NULL)
return 0;
return max(heightOfTree(pRoot->left) ,heightOfTree(pRoot->right)) + 1;
}
};