题目描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树。
题目地址

解决方案

class Solution {
public:
    bool IsBalanced_Solution(TreeNode* pRoot) {
        if(pRoot == NULL) return true;
        if(abs(getDepth(pRoot->left) - getDepth(pRoot->right)) > 1) 
            return false;
        return IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
    }
    
    int getDepth(TreeNode* pRoot){
        if(pRoot==NULL)
            return 0;
        return max(getDepth(pRoot->left),getDepth(pRoot->right))+1;
    }
};