Skip to content

Ch 4. Trees and Graphs/4.4_Check_Balanced.cpp... wrong wrapper function #47

@michaelzeng96

Description

@michaelzeng96

int checkHeight(TreeNode* root) {
if (root == nullptr) {
return 0;
}
int leftHeight = checkHeight(root->left);
if (leftHeight == -1) {
return -1;
}
int rightHeight = checkHeight(root->right);
if (rightHeight == -1) {
return -1;
}

if (abs(leftHeight - rightHeight) > 1) {
	return -1;
}
else {
	return max(leftHeight, rightHeight) + 1;
}

}

//wrapper function should be checkHeight != -1 ... not checkHeight == -1
bool isBalanced(TreeNode* root) {
//return (checkHeight == -1);
return (checkHeight != -1);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions