forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
33 lines (30 loc) · 830 Bytes
/
solution.h
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include <complex>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool isBalanced(TreeNode *root) {
return root == NULL || (isBalanced(root->left)
&& isBalanced(root->right)
&& std::abs(treeHigh(root->left) - treeHigh(root->right)) <= 1);
}
private:
int treeHigh(TreeNode *node)
{
if (node == NULL) return 0;
else return 1+std::max(treeHigh(node->left), treeHigh(node->right));
}
};