-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0110_balanced_binary_tree.cpp
More file actions
67 lines (55 loc) · 1.9 KB
/
0110_balanced_binary_tree.cpp
File metadata and controls
67 lines (55 loc) · 1.9 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// https://leetcode.com/problems/balanced-binary-tree/
// O(n) solution USING THE FAMOUS, EXCEPTION-DRIVEN DEVELOPMENT! It's the new hot shit after test-driven development. Jokes aside, this idea is disgusting, but I literally solved the problem in 3 minutes with it so I guess I cannot complain. I'm a horrible person.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
try {
dfsTravel(root);
} catch (exception exception) {
// lmao, exception-driven development
return false;
}
return true;
}
private:
// @returns the height of the tree
int dfsTravel(TreeNode* node) {
if (node == nullptr) return 0;
int leftHeight = dfsTravel(node->left);
int rightHeight = dfsTravel(node->right);
if (abs(rightHeight - leftHeight) > 1) {
throw std::invalid_argument("Height issue");
}
return 1 + max(leftHeight, rightHeight);
}
};
// O(n) solution, very similar to to the one above, without the Y I K E S exception-usage.
class Solution {
public:
bool isBalanced(TreeNode* root) {
return dfsTravel(root) != -1;
}
private:
int dfsTravel(TreeNode* node) {
if (node == nullptr) return 0;
int leftHeight = dfsTravel(node->left);
if (leftHeight == -1) return -1;
int rightHeight = dfsTravel(node->right);
if (rightHeight == -1) return -1;
if (abs(leftHeight - rightHeight) > 1) {
return -1;
}
return 1 + max(leftHeight, rightHeight);
}
};