-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path110.balanced-binary-tree.cc
More file actions
49 lines (45 loc) · 1.08 KB
/
110.balanced-binary-tree.cc
File metadata and controls
49 lines (45 loc) · 1.08 KB
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
#include "leetcode_common_struct.h"
#include <map>
// Runtime: 32 ms, faster than 28.94% of C++ online submissions for Balanced
// Binary Tree.
// Memory Usage: 25.8 MB, less than 5.59% of C++ online submissions for Balanced
// Binary Tree.
std::map<TreeNode *, std::pair<int, int>> cache;
void GetChildHeight(TreeNode *root, int &left, int &right) {
if (!root) {
left = 0;
right = 0;
return;
}
auto it = cache.find(root);
if (it != cache.end()) {
left = it->second.first;
right = it->second.second;
return;
}
int a = 0;
int b = 0;
left = 0;
if (root->left) {
GetChildHeight(root->left, a, b);
left = std::max(a, b) + 1;
}
right = 0;
if (root->right) {
GetChildHeight(root->right, a, b);
right = std::max(a, b) + 1;
}
cache[root] = std::make_pair(left, right);
}
bool isBalanced(TreeNode *root) {
cache.clear();
int a = 0;
int b = 0;
GetChildHeight(root, a, b);
for (auto it = cache.begin(); it != cache.end(); ++it) {
if (std::abs(it->second.first - it->second.second) > 1) {
return false;
}
}
return true;
}