Skip to content

Commit ce55883

Browse files
Add files via upload
1 parent a8914fb commit ce55883

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

22 - Binary Search Trees/Solution.c

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
typedef struct Node{
4+
struct Node* left;
5+
struct Node* right;
6+
int data;
7+
}Node;
8+
Node* newNode(int data){
9+
Node* node=(Node*)malloc(sizeof(Node));
10+
node->left=node->right=NULL;
11+
node->data=data;
12+
return node;
13+
}
14+
15+
int getHeight(Node* root){
16+
if(!root) {
17+
return -1;
18+
}
19+
int lheight = getHeight(root->left);
20+
int rheight = getHeight(root->right);
21+
22+
return (lheight > rheight ? lheight : rheight) + 1;
23+
}
24+
25+
26+
Node* insert(Node* root,int data){
27+
if(root==NULL)
28+
return newNode(data);
29+
else{
30+
Node* cur;
31+
if(data<=root->data){
32+
cur=insert(root->left,data);
33+
root->left=cur;
34+
}
35+
else{
36+
cur=insert(root->right,data);
37+
root->right=cur;
38+
}
39+
}
40+
return root;
41+
}
42+
//Aditya Seth
43+
int main(){
44+
Node* root=NULL;
45+
int T,data;
46+
scanf("%d",&T);
47+
while(T-->0){
48+
scanf("%d",&data);
49+
root=insert(root,data);
50+
}
51+
int height=getHeight(root);
52+
printf("%d",height);
53+
return 0;
54+
55+
}

22 - Binary Search Trees/Solution.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <cmath>
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
class Node {
7+
public:
8+
int data;
9+
Node *left;
10+
Node *right;
11+
12+
Node(int d) {
13+
data = d;
14+
left = NULL;
15+
right = NULL;
16+
}
17+
};
18+
19+
class Solution {
20+
public:
21+
Node *insert(Node *root, int data) {
22+
if (root == NULL) {
23+
return new Node(data);
24+
} else {
25+
Node *cur;
26+
if (data <= root->data) {
27+
cur = insert(root->left, data);
28+
root->left = cur;
29+
} else {
30+
cur = insert(root->right, data);
31+
root->right = cur;
32+
}
33+
34+
return root;
35+
}
36+
}
37+
38+
int getHeight(Node *root) {
39+
return root == NULL ? -1 : 1 + max(getHeight(root->left), getHeight(root->right));
40+
}
41+
};
42+
//Aditya Seth
43+
int main() {
44+
Solution myTree;
45+
Node *root = NULL;
46+
int t;
47+
int data;
48+
49+
cin >> t;
50+
51+
while (t-- > 0) {
52+
cin >> data;
53+
root = myTree.insert(root, data);
54+
}
55+
int height = myTree.getHeight(root);
56+
cout << height;
57+
58+
return 0;
59+
}

22 - Binary Search Trees/Solution.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.right = self.left = None
4+
self.data = data
5+
6+
7+
class Solution:
8+
def insert(self, root, data):
9+
if root is None:
10+
return Node(data)
11+
else:
12+
if data <= root.data:
13+
cur = self.insert(root.left, data)
14+
root.left = cur
15+
else:
16+
cur = self.insert(root.right, data)
17+
root.right = cur
18+
return root
19+
20+
def getHeight(self, root):
21+
return -1 if root is None else 1 + max(self.getHeight(root.left), self.getHeight(root.right))
22+
23+
#Aditya Seth
24+
T = int(input())
25+
myTree = Solution()
26+
root = None
27+
for i in range(T):
28+
data = int(input())
29+
root = myTree.insert(root, data)
30+
height = myTree.getHeight(root)
31+
print(height)

0 commit comments

Comments
 (0)