AcWing 72. 平衡二叉树
原题链接
简单
作者:
浅唱笑竹神易
,
2024-10-05 22:45:55
,
所有人可见
,
阅读 1
/**
* 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:
int ans(TreeNode *root,bool &f){
if(root==NULL)return 0;
int l=ans(root->left,f);
int r=ans(root->right,f);
if(l-r>1||r-l>1)f=false;
return max(l,r)+1;
}
bool isBalanced(TreeNode* root) {
//但凡一个不是,就都不是
if(root==NULL)return true;
else {
bool f=true;
ans(root,f);
return f;
}
}
};