-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathCheck balanced.cpp
75 lines (57 loc) · 2.01 KB
/
Check balanced.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Check Balanced
Given a binary tree, check if it is balanced. Return true if given binary tree is balanced, false otherwise.
Balanced Binary Tree:
A empty binary tree or binary tree with zero nodes is always balanced. For a non empty binary tree to be balanced, following conditions must be met:
1. The left and right subtrees must be balanced.
2. |hL - hR| <= 1, where hL is the height or depth of left subtree and hR is the height or depth of right subtree.
Input format:
The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space. If any node does not have a left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist, therefore, it will not be a part of the data of any node.
Output format
The first and only line of output contains true or false.
Constraints
Time Limit: 1 second
Sample Input 1 :
5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1
Sample Output 1 :
false
Sample Input 2 :
1 2 3 -1 -1 -1 -1
Sample Output 2 :
true
*/
/**********************************************************
Following is the Binary Tree Node class structure
template <typename T>
class BinaryTreeNode {
public :
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};
***********************************************************/
int height(BinaryTreeNode<int> *root) {
if(root == NULL) {
return 0;
}
return 1 + max(height(root -> left), height(root -> right));
}
bool isBalanced(BinaryTreeNode<int> *root) {
// Write your code here
// corner case
if(root == NULL) {
return true;
}
int left = height(root -> left);
int right = height(root -> right);
int difference = abs(left - right);
if(difference <= 1 and isBalanced(root -> left) and isBalanced(root -> right)){
return true;
}
return false;
}