Skip to content

Commit 03337b5

Browse files
committed
add binary tree codes
1 parent ef41943 commit 03337b5

6 files changed

+181
-0
lines changed

a.out

21.9 KB
Binary file not shown.
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// http://www.geeksforgeeks.org/check-two-nodes-cousins-binary-tree/
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
// struct node of binary tree
6+
struct node{
7+
int data;
8+
node *left, *right;
9+
};
10+
11+
// return new node
12+
node *getnode(int data){
13+
node *temp = new node();
14+
temp->data = data;
15+
temp->left = temp->right = NULL;
16+
}
17+
18+
int checkIfSibling(node *root, int p1, int p2){
19+
if(!root) return 0;
20+
if(root->left && root->right){
21+
if((root->left->data == p1 && root->right->data == p2) ||
22+
(root->left->data == p2 && root->right->data == p1)) return 1;
23+
}
24+
return (checkIfSibling(root->left,p1,p2) || checkIfSibling(root->right,p1,p2));
25+
}
26+
27+
int checkCousin(node *root, int p1, int p2){
28+
int a = -1, b = -1;
29+
queue<node*> q;
30+
if(!root) return 0;
31+
q.push(root);
32+
while(!q.empty()){
33+
int size = q.size();
34+
a = -1, b = -1;
35+
while(size--){
36+
node *temp = q.front();
37+
q.pop();
38+
if(temp->data == p1)
39+
a = 1;
40+
else if(temp->data == p2)
41+
b = 1;
42+
if(temp->left) q.push(temp->left);
43+
if(temp->right) q.push(temp->right);
44+
}
45+
if(a == 1 && b == 1 && !checkIfSibling(root,p1,p2))
46+
return 1;
47+
}
48+
return 0;
49+
}
50+
51+
int main(){
52+
//contruct binary tree
53+
node *root = getnode(1);
54+
root->left = getnode(2);
55+
root->left->left = getnode(4);
56+
root->left->right = getnode(5);
57+
root->left->left->right = getnode(2);
58+
root->right = getnode(3);
59+
root->right->right = getnode(8);
60+
root->right->right->left = getnode(6);
61+
root->right->right->right = getnode(7);
62+
63+
int p1 = 4, p2 = 8;
64+
if(checkCousin(root,p1,p2)) cout << "yes\n";
65+
else cout << "No\n";
66+
67+
return 0;
68+
}

egg-dropping-puzzle

11.3 KB
Binary file not shown.

egg-dropping-puzzle.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// http://practice.geeksforgeeks.org/problems/egg-dropping-puzzle/0
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
int main(){
5+
int t,n,k;
6+
cin>>t;
7+
while(t--){
8+
cin>>n>>k;
9+
10+
}
11+
return 0;
12+
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// http://www.geeksforgeeks.org/find-sum-left-leaves-given-binary-tree/
2+
3+
// CPP program to find total sum
4+
// of right leaf nodes
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
8+
// struct node of binary tree
9+
struct node{
10+
int data;
11+
node *left, *right;
12+
};
13+
14+
// return new node
15+
node *getnode(int data){
16+
node *temp = new node();
17+
temp->data = data;
18+
temp->left = temp->right = NULL;
19+
}
20+
21+
// utility function to calculate sum
22+
// of right leaf nodes
23+
void rightLeafSum(node *root, int *sum){
24+
if(!root)
25+
return;
26+
27+
// check if the right child of root
28+
// is leaf node
29+
if(root->right)
30+
if(!root->right->left && !root->right->right)
31+
*sum += root->right->data;
32+
33+
rightLeafSum(root->left, sum);
34+
rightLeafSum(root->right, sum);
35+
}
36+
37+
// driver program
38+
int main(){
39+
//contruct binary tree
40+
node *root = getnode(1);
41+
root->left = getnode(2);
42+
root->left->left = getnode(4);
43+
root->left->right = getnode(5);
44+
root->left->left->right = getnode(2);
45+
root->right = getnode(3);
46+
root->right->right = getnode(8);
47+
root->right->right->left = getnode(6);
48+
root->right->right->right = getnode(7);
49+
50+
// variable to store sum of right leaves
51+
int sum = 0;
52+
rightLeafSum(root, &sum);
53+
cout << sum << endl;
54+
return 0;
55+
}

manipulate-string.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Given two strings return the value of least number of manipulations needed to ensure both strings have identical characters.
3+
For input aab aba it’s 0, for input abc cdd it is 2
4+
5+
6+
*/
7+
8+
// CPP program to count least number
9+
// of manipulations to have two strings
10+
// set of same characters
11+
#include <bits/stdc++.h>
12+
using namespace std;
13+
14+
// return the count of manipulations
15+
// required
16+
int leastCount(string s1, string s2, int n){
17+
int count1[26] = {0};
18+
int count2[26] = {0};
19+
20+
// count the number of different
21+
// characters in both strings
22+
for(int i = 0; i < n; i++){
23+
count1[s1[i] - 'a'] += 1;
24+
count2[s2[i] - 'a'] += 1;
25+
}
26+
27+
// check the difference of character
28+
// by comparing count arrays
29+
int res = 0;
30+
for(int i = 0; i < 26; i++){
31+
if(count1[i] != 0){
32+
res += abs(count1[i] - count2[i]);
33+
}
34+
}
35+
return res;
36+
}
37+
38+
// driver program
39+
int main(){
40+
string s1 = "cdd";
41+
string s2 = "abc";
42+
int len = s1.length();
43+
int res = leastCount(s1,s2,len);
44+
cout << res << endl;
45+
return 0;
46+
}

0 commit comments

Comments
 (0)