-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path110-Balanced Binary Tree.cpp
executable file
·54 lines (46 loc) · 1.57 KB
/
110-Balanced Binary Tree.cpp
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
45
46
47
48
49
50
51
52
53
54
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
unordered_map<TreeNode*, int> depth;
getDepth(depth, root);
return isBalanced(depth, root);
}
private:
bool isBalanced(unordered_map<TreeNode*, int> &depth, TreeNode* root) {
if (root == nullptr) return true;
return abs(depth[root->left] - depth[root->right]) <= 1
&& isBalanced(root->left) && isBalanced(root->right);
}
int getDepth(unordered_map<TreeNode*, int> &depth, TreeNode* root) {
if (root == nullptr) return 0;
if (depth.find(root) != depth.end()) return depth[root];
int leftDepth = getDepth(depth, root->left);
int rightDepth = getDepth(depth, root->right);
return depth[root] = (max(leftDepth, rightDepth) + 1);
}
};
// faster and shorter solution
class Solution {
public:
bool isBalanced(TreeNode* root) {
return solve(root).first;
}
private:
pair<bool, int> solve(TreeNode *root) {
if (root == nullptr) return {true, 0};
pair<bool, int> left = solve(root->left);
if (!left.first) return left;
pair<bool, int> right = solve(root->right);
if (!right.first) return right;
return {abs(left.second - right.second) <= 1, max(left.second, right.second) + 1};
}
};