diff --git a/Algorithms/Tree/Height of the tree/CPP/Height of BST in C++.cpp b/Algorithms/Tree/Height of the tree/CPP/Height of BST in C++.cpp new file mode 100644 index 00000000..5df2a9e0 --- /dev/null +++ b/Algorithms/Tree/Height of the tree/CPP/Height of BST in C++.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +class node +{ + public: + int data; + node* left; + node* right; +}; + + +int maxDepth(node* node) +{ + if (node == NULL) + return 0; + else + { + + int lDepth = maxDepth(node->left); + int rDepth = maxDepth(node->right); + + if (lDepth > rDepth) + return(lDepth + 1); + else return(rDepth + 1); + } +} + +node* newNode(int data) +{ + node* Node = new node(); + Node->data = data; + Node->left = NULL; + Node->right = NULL; + + return(Node); +} + + +int main() +{ + node *root = newNode(1); + + root->left = newNode(2); + root->right = newNode(3); + root->left->left = newNode(4); + root->left->right = newNode(5); + + cout << "Height of tree is " << maxDepth(root); + return 0; +} +