-
Notifications
You must be signed in to change notification settings - Fork 5
/
q3.cpp
82 lines (71 loc) · 1.49 KB
/
q3.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
76
77
78
79
80
81
82
// C++ program of the
// above approach
#include <bits/stdc++.h>
using namespace std;
// Structure of
// binary tree node
struct Node {
int data;
Node *left, *right;
};
// Function to create new node
Node* newNode(int data)
{
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Utility function which calculates
// smallest three nodes of all leaf nodes
void storeLeaf(Node* root, vector<int>& arr)
{
if (!root)
return;
// Check if current root is a leaf node
if (!root->left and !root->right) {
arr.push_back(root->data);
return;
}
// Traverse the left
// and right subtree
storeLeaf(root->left, arr);
storeLeaf(root->right, arr);
}
// Function to find the K smallest
// nodes of the Binary Tree
void KSmallest(Node* root, int k)
{
vector<int> arr;
storeLeaf(root, arr);
// Sorting the Leaf nodes array
sort(arr.begin(), arr.end());
// Loop to print the K smallest
// Leaf nodes of the array
for (int i = 0; i < k; i++) {
if (i < arr.size()) {
cout << arr[i] << " ";
}
else {
break;
}
}
}
// Driver Code
int main()
{
// Construct binary tree
Node* root = newNode(1);
root->left = newNode(2);
root->left->left = newNode(4);
root->left->left->left = newNode(21);
root->left->right = newNode(5);
root->left->right->right = newNode(8);
root->right = newNode(3);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->right->right->right = newNode(19);
// Function Call
KSmallest(root, 3);
return 0;
}