Get the Height of a Node in a Binary Tree

Objective: Given a binary tree, find the height of a given node in the tree.

Input: A Binary Tree and a node

Output: Height of a given node in the tree.

Example:

Get The Height Of a Node

Get The Height Of a Node

Approach:

Recursion:

  • Take a variable called height =0.
  • Search for that given node in the tree using recursion.
  • Each time you left or right , increase the height by 1.
  • Once you found the given node, return the height.
  • If till the end you wont find the node, return 0

Complete Code:

Output :
Height of the Node 25 is : 3

5 Responses

  1. sunny kansal says:

    There’s something not correct. Height of Node x => 25 should be 1 not 3. Can you check it please?

    • tutorialhorizon says:

      We are considering the height here as number of nodes above the particular node (including the node itself)

      • sunny kansal says:

        Still output and solution is wrong…Height of Node is the longest path till its leaf node…And it should be 1 for Node 25 not 3…but the number of levels are 3…

  2. arefin nowshad says:

    This code seems like calculating depth of a node. Even that is actually wrong, depth should be 2 for Node 25 and height is 1. You cant consider the height as the number of nodes above the particular node including the node itself as the right way to define height of a node. Because this is outright wrong.
    http://stackoverflow.com/questions/2603692/what-is-the-difference-between-tree-depth-and-height

  3. crackerplace says:

    yes even in this problem http://algorithms.tutorialhorizon.com/find-the-maximum-depth-or-height-of-a-binary-tree/ ,the assumption of what is height is confusing and misleading as per the general standards.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: