-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathsolve.cpp
More file actions
35 lines (34 loc) · 782 Bytes
/
solve.cpp
File metadata and controls
35 lines (34 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdlib>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
static inline int abs(int a, int b)
{
return a > b ? a - b : b - a;
}
class Solution {
public:
bool isBalanced(TreeNode *root) {
bool unbalenced = false;
getHeight(unbalenced, root);
return !unbalenced;
}
int getHeight(bool &unbalenced, TreeNode *root) {
if (root == NULL || unbalenced)
return -1;
int left = getHeight(unbalenced, root->left);
int right = getHeight(unbalenced, root->right);
if (abs(left, right) > 1) {
unbalenced = true;
}
return 1 + (left > right ? left : right);
}
};
int main(int argc, char **argv)
{
return 0;
}