/**
* 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 发布
本文介绍了一种用于判断二叉树是否为平衡二叉树的算法实现。通过递归方式计算每个节点左右子树的高度,并检查高度差是否超过1来判断平衡性。此方法高效且易于理解。
247

被折叠的 条评论
为什么被折叠?



