Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
michaelzeng96 opened this issue Nov 20, 2017 · 0 comments

Comments

@michaelzeng96
Copy link

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant