-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc7f121
commit 6924fd2
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |