Skip to content

Commit

Permalink
new ques in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
arorarahul committed May 17, 2017
1 parent fc7f121 commit 6924fd2
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ always generate a unique tree.
- [Print left view of a binary tree](/trees/question17.c)
- [Remove all paths of length k from root in a binary tree](/trees/question18.c)
- [Check whether a given binary tree if a subtree of another binary tree](/trees/question19.c)
- [Check whether given two nodes are cousins in a binary tree](/trees/question20.c)
- [Form a balanced binary search tree from a given sorted array](/trees/question21.c)

## Some important concepts to solve algos better

Expand Down Expand Up @@ -306,6 +308,8 @@ trees having random number of children not necessary equal
- Vertical tree order traversal: Root is at a distance 0 from itself. When we move to the left child it is at a distance -1 from the root, the right child is at a distance 1 from the root. If we keep doing -1 for every left child and +1 for every right child, we will have multiple nodes at the same distance value. The nodes having same distance value fall on a vertical line. If we traverse that line, it is called vertical tree order traversal
- Sum tree is the one where sum of values in the LST and sum of values in the RST is equal to the root. This is valid for all the nodes except the tree
- Recursion execution stack only remembers the next line if there is no return statement, else it will return the execution stack
- Two nodes in a tree are siblings if they are at the same level and have same parent, are cousins if they
are at same level but do not have same parent

# C Programming - Topic1: Introduction

Expand Down
78 changes: 78 additions & 0 deletions trees/question20.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Check whether given two nodes are cousins in a binary tree
Two nodes in a tree are siblings if they are at the same level and have same parent, are cousins if they
are at same level but do not have same parent
METHOD1:
Method1 is that we can check for the level at which both the nodes are present. If the levels are same, we
can check for the parent. Eg: if the level if 4, we will just go till level 3 to check for parents.
Time complexity: O(n) + O(n) = O(n) //for finding level + traversal for parents
*/

#include <stdio.h>
#include <stdlib.h>

struct node{
int data;
struct node *left;
struct node *right;
};

struct node *newNode(int data){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->left = temp->right= NULL;
return temp;
}

int checkHeight(struct node *root, int num1, int k){
if(!root){
return 0;
}
if(root->data == num1){
return k;
}
int left = checkHeight(root->left, num1, k+1);
int right = checkHeight(root->right, num1, k+1);
return left?left:right;
}

int checkParent(struct node *root, int num1, int num2, int height, int k){
if(!root){
return 1;
}
if(k == height-1 && root->left->data == num1 && root->right->data == num2){
return 0;
}
return (checkParent(root->left, num1, num2, height,k+1) && checkParent(root->right, num1, num2, height,k+1));
}

int checkCousins(struct node *root, int num1, int num2){
int k = 0;
int height1 = checkHeight(root,num1, k);//checking if they are at the same level or not
int height2 = checkHeight(root,num2, k);//checking if they are at the same level or not
if(height1 == height2){
return checkParent(root, num1,num2,height1,k);
}
return 0;
}

int main(){
struct node *root = newNode(10);
root->left = newNode(12);
root->left->left = newNode(14);
root->left->right = newNode(16);
root->right = newNode(20);
root->right->left = newNode(22);
root->right->right = newNode(26);

int cousins = checkCousins(root,14,22);
printf("cousin value is %d\n", cousins);
if(cousins){
printf("Yes they are cousins\n");
}else{
printf("NOT cousins\n");
}
}
71 changes: 71 additions & 0 deletions trees/question21.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Form a balanced binary search tree from a given sorted array
Since a sorted array will always give a skewed tree if done directly,
METHOD1:
To avoid skewed tree, if we check for every insertion if the tree needs to be rotated in order to be
balanced. For each element we will have to check logn level for thats
Time complexity: O(nlogn) //n is the total number of elements in the array
Space complexity: O(1) //if iterative
METHOD2:
We can simply pick the middle element and elements to the left of it will be LST and to the right of it
will be RST, then again from elements to the left we pick middle elemenet and keep repeating the process.
Time complexity T(n) = 1+T(n/2)+T(n/2) = O(n) //picking middle + doing the same process on LST and RST
Space complexity = O(1) // iterative
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct node{
int data;
struct node *left;
struct node *right;
};

struct node *newNode(int data){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}

void printTree(struct node *root){
if(root){

printTree(root->left);
printf("%d\n", root->data);
printTree(root->right);

}
}

struct node *makeBBST(int *arr,struct node *root, int start,int end){
if(start > end){
return NULL;
}
int middle;
if(start == end){
middle = start;
}else{
middle = floor(end-start/2);
}
root = newNode(arr[middle]);
root->left = makeBBST(arr,root,0,middle-1);
root->right = makeBBST(arr,root,middle+1,end);
return root;
}

int main(){
struct node *root = NULL;
int arr[] = {10,20,30,40,50,60,70};
int size = sizeof(arr)/sizeof(arr[0]);
root = makeBBST(arr, root,0,size-1);
printf("RAHUL ARORA %d\n", root->left->data);
printTree(root);
}

0 comments on commit 6924fd2

Please sign in to comment.