leetcode 110 : 平衡二叉树

题目

《leetcode 110 : 平衡二叉树》

算法思想 :我们只需要递归的判断每个根节点的左右子树高度是否小于等于1即可。

 

int maxDepth(TreeNode* root) {
    int high = 0;
    if(root == NULL)
        high = 0; 
    else
        high = max(maxDepth(root->left),maxDepth(root->right) ) + 1; 
    return high;
}

bool isBalanced(TreeNode* root) {
    if(root == NULL)
        return true;
    int l_h = maxDepth(root->left);
    int r_h = maxDepth(root->right);
    if(abs(l_h-r_h) > 1)
        return false;
    else
        return (isBalanced(root->left) && isBalanced(root->right) );
    return true; 
}

    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/wh_computers/article/details/82318019
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞