/**
* 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,int &h){//h是高度
if(!root) {h=0;return true;}
int hl,hr;
return isBalanced(root->left,hl) && isBalanced(root->right,hr) && (h=max(hl+1,hr+1),abs(hl-hr)<=1);
}
bool isBalanced(TreeNode* root) {
int h;
return isBalanced(root,h);
}
};
Leetcode 110. 平衡二叉树
最新推荐文章于 2024-04-11 23:52:20 发布