-
Notifications
You must be signed in to change notification settings - Fork 1
/
110_Balanced_Binary_Tree.cpp
61 lines (49 loc) · 1.3 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
55
56
57
58
59
60
61
// https://leetcode.com/problems/balanced-binary-tree/
// https://leetcode.com/problems/balanced-binary-tree/discuss/2008116/C%2B%2B-O(N)-Solution-or-checking-condition-while-calculating-depthheight-_
#include <iostream>
using namespace std;
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) {}
};
int helper(TreeNode *root, bool &flag)
{
if (root == NULL)
return 0;
int left = helper(root->left, flag);
int right = helper(root->right, flag);
// checking for balance
if (abs(left - right) > 1)
{
flag = false;
return 0;
}
// calculating height
return max(left, right) + 1;
}
bool isBalanced(TreeNode *root)
{
bool ans = true;
helper(root, ans);
return ans;
}
int main()
{
// 1
// 2 3
// 4 5 6 7
TreeNode *p = new TreeNode(1);
p->left = new TreeNode(2);
p->right = new TreeNode(3);
p->left->left = new TreeNode(4);
p->left->right = new TreeNode(5);
p->right->left = new TreeNode(6);
p->right->right = new TreeNode(7);
cout << isBalanced(p) << endl;
return 0;
}