Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 27, 2018 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/3a6e9064c17529e4b37933c45bf87481 to your computer and use it in GitHub Desktop.
Save jianminchen/3a6e9064c17529e4b37933c45bf87481 to your computer and use it in GitHub Desktop.
Leetcode 110 - balanced tree - July 25, 2018 mock interview 10:00 PM - less than 15 minutes.
#include <iostream>
using namespace std;
// To execute C++, please define "int main()"
int main() {
// Construct tree
TreeNode *root; // assign
bool answer = true;
IsHeightBalanced(root, answer); // no need catch
cout << answer << endl;
return 0;
}
int IsHeightBalanced(TreeNode *root, bool &answer)
{
if (root)
{
int leftSubtreeHeight = IsHeightBalanced(root->left, answer);
int rightSubtreeHeight = IsHeightBalanced(root->right, answer);
if (abs(leftSubtreeHeight - rightSubtreeHeight) > 1)
{
answer = false;
}
return 1 +
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment