【Leetcode二叉树属性五】110. 平衡二叉树

文章目录

  • Leetcode110
    • 1.问题描述
    • 2.解决方案

Leetcode110

1.问题描述

【Leetcode二叉树属性五】110. 平衡二叉树_第1张图片

2.解决方案

class Solution {
     
public:
    int getHeight(TreeNode* root){
     
        if(root== nullptr) return 0;
        return max(getHeight(root->left), getHeight(root->right))+1;
    }
    bool isBalanced(TreeNode* root) {
     
        if(root== nullptr) return true;
        if(abs(getHeight(root->left)- getHeight(root->right))>1) return false;
        return isBalanced(root->left)&& isBalanced(root->right);
    }
};

你可能感兴趣的:(#,二叉树,leetcode,算法)