Created
May 9, 2016 23:57
-
-
Save jianminchen/18de7257b29bf6ccbab63cdc93d4077f to your computer and use it in GitHub Desktop.
HackerRank - Tree - height of tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*The tree node has data, left child and right child | |
struct node | |
{ | |
int data; | |
node* left; | |
node* right; | |
}; | |
*/ | |
int height(node * root) | |
{ | |
if(root==NULL) | |
return 0; | |
if(root!=NULL && root->left==NULL && root->right==NULL) | |
return 0; | |
int hl = height(root->left); | |
int hr = height(root->right); | |
return hl>=hr?(1+hl):(1+hr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment