问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
class Solution {
public:
int getdepth(TreeNode* root){
if(root == nullptr) return 0;
return 1+max(getdepth(root->left),getdepth(root->right));
}
public:
bool isBalanced(TreeNode* root) {
if(root == nullptr) return true;
bool left = isBalanced(root->left);
bool right = isBalanced(root->left);
if(abs(getdepth(root->left)-getdepth(root->right))>1)
return false;
else
return left&&right;
}
};