#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
struct TreeNode{
int val;
TreeNode* right;
TreeNode* left;
TreeNode() :val(0), right(nullptr), left(nullptr){}
};
class MyClass
{
public:
bool isBalanced(TreeNode* root);
int getDepth(TreeNode* node);
};
bool MyClass::isBalanced(TreeNode* root){
if (!root){
return true;
}
if (abs(getDepth(root->right) - getDepth(root->left)) >1){
return false;
}
return isBalanced(root->left) && isBalanced(root->right);
}
int MyClass::getDepth(TreeNode* node){
if (!node){
return 0;
}
int left = getDepth(node->left);
int right = getDepth(node->right);
return max(left, right) + 1;
}
验证平衡二叉树
最新推荐文章于 2023-01-30 19:33:53 发布