Tree: Height of a Binary Tree

PROBLEM :

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. This means that a tree containing a single node has a height of .

Complete the height function provided in your editor so that it returns the height of a binary tree. This function has a parameter, , which is a pointer to the root node of a binary tree.

Input Format

You do not need to read any input from stdin. Our grader will pass the root node of a binary tree to your height function.

Output Format

Your function should return a single integer denoting the height of the binary tree.

Sample Input

4
       /   \
     2     7
    / \   /
  1   3 6

output : 2

Note: A binary search tree is a binary tree in which the value of each parent node's left child is less than the value the parent node, and the value of the parent node is less than the value of its right child.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------


/*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->left==NULL&&root->right==NULL)
        return 0 ;
    int L,R ;
    L=height(root->left) ;
    R=height(root->right) ;
 
    return (L>R?L+1:R+1) ;
}



---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )