2023每日刷题(十九)
Leetcode—110.平衡二叉树
实现代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int preFunc(struct TreeNode* root) {
if(root == NULL) {
return 0;
}
int left = preFunc(root->left);
if(left == -1) {
return -1;
}
int right = preFunc(root->right);
if(right == -1 || abs(right - left) > 1) {
return -1;
}
return (left > right ? left : right) + 1;
}
bool isBalanced(struct TreeNode* root) {
return preFunc(root) != -1;
}
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!