mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
fix(avl tree): fix all issue
This commit is contained in:
@ -34,14 +34,14 @@ bool AvlTree::isBalance(const TreeNode *p) {
|
||||
// 获取当前结点的平衡因子
|
||||
int AvlTree::getBalanceFactor(const TreeNode *p) {
|
||||
if (p->left == nullptr && p->right == nullptr) { return 0; }
|
||||
else if (p->left == nullptr) { return (-p->right->height); }
|
||||
else if (p->right == nullptr) { return p->left->height; }
|
||||
else if (p->left == nullptr) { return (-1 - p->right->height); }
|
||||
else if (p->right == nullptr) { return p->left->height + 1; }
|
||||
else { return p->left->height - p->right->height; }
|
||||
}
|
||||
|
||||
// 更新结点高度
|
||||
void AvlTree::updateHeight(TreeNode *p) {
|
||||
if (p->left == nullptr && p->right == nullptr) { p->height = 1; }
|
||||
if (p->left == nullptr && p->right == nullptr) { p->height = 0; }
|
||||
else if (p->left == nullptr) { p->height = p->right->height + 1; }
|
||||
else if (p->right == nullptr) { p->height = p->left->height + 1; }
|
||||
else { p->height = std::max(p->left->height, p->right->height) + 1; }
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
*/
|
||||
struct TreeNode {
|
||||
int val{};
|
||||
int height = 1;
|
||||
int height = 0;
|
||||
TreeNode *parent{};
|
||||
TreeNode *left{};
|
||||
TreeNode *right{};
|
||||
|
||||
Reference in New Issue
Block a user