Hackerrank | 30 days of code | Day 22-Solution in c++| Binary-Search-Trees-hackerrank-Solution-in-c++


Hackerrank | 30 days of code | Day 22-Solution in c++| Binary-Search-Trees-hackerrank-Solution-in-c++

Hackerrank Solution 30-days-of-code
https://www.hackerrank.com/challenges/30-binary-search-trees/problem
Day 22 Binary Search Trees Solution in Cpp
#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){ if ( !root ) { return -1; } int right_height = getHeight(root->right); int left_height = getHeight(root->left); if (right_height > left_height) { return (right_height + 1); } return (left_height + 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; }

Post a Comment

0 Comments