/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root==NULL)
return true;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
if(abs(left-right) > 1)
return false;
return isBalanced(root->left)&&isBalanced(root->right);
}
int maxDepth(TreeNode* root) {
if(root==NULL)
return 0;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return max(left,right)+1;
}
};
110 Balanced Binary Tree
最新推荐文章于 2025-01-06 13:49:47 发布