Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Day22-Binary_Search_Trees.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <cstddef>

using namespace std;

class Node{
public:
int data;
Node *left;
Node *right;
Node(int d){
data = d;
left = NULL;
right = NULL;
}
};
class Solution{
public:
Node* insert(Node* root, int data) {
if(root == NULL) {
return new Node(data);
}
else {
Node* cur;
if(data <= root->data){
cur = insert(root->left, data);
root->left = cur;
}
else{
cur = insert(root->right, data);
root->right = cur;
}

return root;
}
}

int getHeight(Node* root){
//Write your code here
if(!root) {
return -1;
}
int leftDepth = getHeight(root->left);
int rightDepth = getHeight(root->right);

return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
}

}; //End of Solution
int main() {
Solution myTree;
Node* root = NULL;
int t;
int data;

cin >> t;

while(t-- > 0){
cin >> data;
root = myTree.insert(root, data);
}
int height = myTree.getHeight(root);
cout << height;

return 0;
}


By GiTan7