《剑指offer》-判断平衡二叉树

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

考察平衡树的概念和递归的使用。平衡树是指,树中的每个节点的左右子树的高度差小于等于1。

class Solution {
public:
    bool IsBalanced_Solution(TreeNode* pRoot) {
		if(pRoot == NULL){
            return true;
        }
        if(IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right)){
            int left_depth = getDepth(pRoot->left);
            int right_depth = getDepth(pRoot->right);
            if(abs(left_depth-right_depth)<=1){
                return true;
            }
            return false;
        }
        return false;
    }
    int getDepth(TreeNode* pRoot){
        if(pRoot == NULL){
            return 0;
        }
        return 1+max(getDepth(pRoot->left), getDepth(pRoot->right));
    }
};
原文地址:https://www.cnblogs.com/zjutzz/p/6501682.html