Skip to content

Commit

Permalink
Height of a tree
Browse files Browse the repository at this point in the history
  • Loading branch information
NripeshKumar committed Oct 11, 2019
1 parent 07cbf20 commit 35b9705
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Algorithms/Tree/Height of the tree/CPP/Height of BST in C++.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <bits/stdc++.h>
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;
}

0 comments on commit 35b9705

Please sign in to comment.