forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.cpp
68 lines (54 loc) · 1.29 KB
/
sol.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
#include <iostream>
#include <vector>
using namespace std;
struct node{
// Binary Tree data structure
int data;
node *left, *right;
};
node *createNode(int val){
// Function to create a binary tree node
node *temp = new node;
temp->data = val;
temp->left = temp->right = NULL;
return temp;
}
int maxDepth(node *root){
// Returns the maximum depth of a binary tree
if (root == NULL) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
void printAllKeysOfLevel(node *root, int level){
// prints all keys of a b. tree at a level
if (root==NULL) return;
if (level==1){
cout << root->data << endl;
}
if (level>1){
printAllKeysOfLevel(root->left, level-1);
printAllKeysOfLevel(root->right, level-1);
}
return;
}
void levelWisePrint(node *root){
// Prints the b. tree level wise
int depth = maxDepth(root);
for (int d=1; d<=depth; d++){
cout << "Level = " << d << endl;
printAllKeysOfLevel(root, d);
}
}
void test(){
// Create and run a test case
node *root = createNode(3);
root->left = createNode(-3);
root->right = createNode(7);
root->left->left = createNode(30);
root->left->right = createNode(50);
root->right->left = createNode(-100);
levelWisePrint(root);
}
int main(){
// runs the test
test();
}