Created
July 27, 2018 06:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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