Assignment 3
Assignment 3
Part 1:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
if (root == NULL)
return;
inorder(root->left);
printf("%d " ,root->data);
inorder(root->right);
return 1;
return 0;
else {
return 1;
else
return 0;
int main()
root1->left = newNode(3);
root1->right = newNode(8);
root1->left->left = newNode(2);
root1->left->right = newNode(4);
root2->left = newNode(3);
root2->right = newNode(8);
root2->left->left = newNode(2);
root2->left->right = newNode(4);
if (isIdentical(root1, root2))
else
return 0;
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
int h = height(root);
int i;
printCurrentLevel(root, i);
if (root == NULL)
return;
if (level == 1)
if (node == NULL)
return 0;
else {
else
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printLevelOrder(root);
return 0;
}
3) find next node in same level for given node in a binary tree (C++ language):
#include <iostream>
#include <list>
struct Node
int data;
Node(int data)
this->data = data;
};
if (root == nullptr) {
return nullptr;
list<Node*> queue;
queue.push_back(root);
while (size--)
front = queue.front();
queue.pop_front();
if (front == node)
if (size == 0) {
return nullptr;
return queue.front();
if (front->left) {
queue.push_back(front->left);
if (front->right) {
queue.push_back(front->right);
return nullptr;
}
int main()
if (right) {
else {
return 0;
4) Determine if a given binary tree is a sub tree of another binary tree or not (C language):
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
return true;
return false;
if (S == NULL)
return true;
if (T == NULL)
return false;
if (areIdentical(T, S))
return true;
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
int main()
T->right = newNode(3);
T->right->right = newNode(3);
T->left = newNode(10);
T->left->left = newNode(4);
T->left->left->right = newNode(30);
T->left->right = newNode(6);
S->right = newNode(6);
S->left = newNode(4);
S->left->right = newNode(30);
if (isSubtree(T, S))
else
getchar();
return 0;
5) Check if given binary tree has symmetric structure or not (C++ language):
#include<bits/stdc++.h>
struct Node
int key;
};
temp->key = key;
if(root == NULL)
return true;
// it is a symmetric tree.
return true;
queue <Node*> q;
q.push(root);
q.push(root);
// symmetry.
while(!q.empty()){
// their symmetry.
leftNode = q.front();
q.pop();
rightNode = q.front();
q.pop();
if(leftNode->key != rightNode->key){
return false;
// node in queue.
q.push(leftNode->left);
q.push(rightNode->right);
// is not symmetric.
return false;
// in queue.
q.push(rightNode->left);
// is not symmetric.
return false;
return true;
// Driver program
int main()
root->left = newNode(2);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(4);
root->right->left = newNode(4);
root->right->right = newNode(3);
if(isSymmetric(root))
else
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
void mirror(struct Node* node)
if (node == NULL)
return;
else {
mirror(node->left);
mirror(node->right);
temp = node->left;
node->left = node->right;
node->right = temp;
if (node == NULL)
return;
inOrder(node->left);
inOrder(node->right);
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
inOrder(root);
mirror(root);
" is \n");
inOrder(root);
return 0;
7) Check if binary tree can be converted to another by doing any no. of swaps of left and right child
(C++ language):
#include <iostream>
struct Node
int data;
this->data = data;
};
if (X == Y) {
return true;
int main()
Node* X = nullptr;
X = new Node(6);
Node* Y = nullptr;
Y = new Node(6);
if (equal(X, Y)) {
else {
return 0;
}
8) Find lowest common ancestor of two nodes in a binary tree (C++ language):
#include <bits/stdc++.h>
struct Node {
int key;
};
Node* newNode(int k)
temp->key = k;
return temp;
// base case
if (root == NULL)
return false;
path.push_back(root->key);
if (root->key == k)
return true;
if ((root->left && findPath(root->left, path, k))
return true;
path.pop_back();
return false;
return -1;
int i;
if (path1[i] != path2[i])
break;
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
return 0;
Part 2: