-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution110.cpp
More file actions
44 lines (37 loc) · 753 Bytes
/
solution110.cpp
File metadata and controls
44 lines (37 loc) · 753 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
36
37
38
39
40
41
42
43
44
/**
* Balanced Binary Tree
*
* cpselvis([email protected])
* September 9th, 2016
*/
#include<iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool isBalanced(TreeNode* root) {
if (root == NULL)
{
return true;
}
int leftDepth = depth(root -> left);
int rightDepth = depth(root -> right);
return abs(leftDepth - rightDepth) <= 1 && isBalanced(root -> left) && isBalanced(root -> right);
}
int depth(TreeNode *root)
{
if (root == NULL)
{
return 0;
}
return max(depth(root -> left), depth(root -> right)) + 1;
}
};
int main(int argc, char **argv)
{
}