/**
* 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 ok;
int dfs(TreeNode *root){
if(root == NULL || !ok) return 0;
int x = dfs(root->left);
int y = dfs(root->right);
if(abs(x-y) > 1) ok = false;
return max(x,y)+1;
}
bool isBalanced(TreeNode* root) {
ok = true;
dfs(root);
return ok;
}
};
leetcode 110. Balanced Binary Tree
最新推荐文章于 2023-03-23 10:33:05 发布