Skip to content

Commit 7d922ea

Browse files
Merge pull request #46 from RohanVolety/main
Added Tree Questions
2 parents 70c5e05 + 2e63d9c commit 7d922ea

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

Trees/Diameter_of_tree.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
/* Tree node structure used in the program */
5+
struct Node
6+
{
7+
int data;
8+
Node *left, *right;
9+
};
10+
11+
/* Function to find height of a tree */
12+
int height(Node *root, int &ans)
13+
{
14+
if (root == NULL)
15+
return 0;
16+
17+
int left_height = height(root->left, ans);
18+
19+
int right_height = height(root->right, ans);
20+
21+
// update the answer, because diameter of a
22+
// tree is nothing but maximum value of
23+
// (left_height + right_height + 1) for each node
24+
ans = max(ans, 1 + left_height + right_height);
25+
26+
return 1 + max(left_height, right_height);
27+
}
28+
29+
/* Computes the diameter of binary tree with given root. */
30+
int diameter(Node *root)
31+
{
32+
if (root == NULL)
33+
return 0;
34+
int ans = INT_MIN; // This will store the final answer
35+
height(root, ans);
36+
return ans;
37+
}
38+
39+
struct Node *newNode(int data)
40+
{
41+
struct Node *node = new Node;
42+
node->data = data;
43+
node->left = node->right = NULL;
44+
45+
return (node);
46+
}
47+
48+
// Driver code
49+
int main()
50+
{
51+
struct Node *root = newNode(1);
52+
root->left = newNode(2);
53+
root->right = newNode(3);
54+
root->left->left = newNode(4);
55+
root->left->right = newNode(5);
56+
57+
printf("Diameter is %d\n", diameter(root));
58+
59+
return 0;
60+
}

Trees/TopView_of_tree.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// C++ program to print top
2+
// view of binary tree
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
// Structure of binary tree
8+
struct Node
9+
{
10+
Node *left;
11+
Node *right;
12+
int hd;
13+
int data;
14+
};
15+
16+
// function to create a new node
17+
Node *newNode(int key)
18+
{
19+
Node *node = new Node();
20+
node->left = node->right = NULL;
21+
node->data = key;
22+
return node;
23+
}
24+
25+
// function should print the topView of
26+
// the binary tree
27+
void topview(Node *root)
28+
{
29+
if (root == NULL)
30+
return;
31+
queue<Node *> q;
32+
map<int, int> m;
33+
int hd = 0;
34+
root->hd = hd;
35+
36+
// push node and horizontal distance to queue
37+
q.push(root);
38+
39+
cout << "The top view of the tree is : \n";
40+
41+
while (q.size())
42+
{
43+
hd = root->hd;
44+
45+
// count function returns 1 if the container
46+
// contains an element whose key is equivalent
47+
// to hd, or returns zero otherwise.
48+
if (m.count(hd) == 0)
49+
m[hd] = root->data;
50+
if (root->left)
51+
{
52+
root->left->hd = hd - 1;
53+
q.push(root->left);
54+
}
55+
if (root->right)
56+
{
57+
root->right->hd = hd + 1;
58+
q.push(root->right);
59+
}
60+
q.pop();
61+
root = q.front();
62+
}
63+
64+
for (auto i = m.begin(); i != m.end(); i++)
65+
{
66+
cout << i->second << " ";
67+
}
68+
}
69+
70+
71+
int main()
72+
{
73+
74+
Node *root = newNode(1);
75+
root->left = newNode(2);
76+
root->right = newNode(3);
77+
root->left->right = newNode(4);
78+
root->left->right->right = newNode(5);
79+
root->left->right->right->right = newNode(6);
80+
cout << "Following are nodes in top view of Binary "
81+
"Tree\n";
82+
topview(root);
83+
return 0;
84+
}

0 commit comments

Comments
 (0)