Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Get the Height of a Node in a Binary Tree

Objec­tive: Given a binary tree, find the height of a given node in the tree.

Input: A Binary Tree and a node

Out­put: Height of a given node in the tree.

Exam­ple:

Get The Height Of a Node

Get The Height Of a Node

Approach:

Recur­sion:

  • Take a vari­able 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

Com­plete Code:

Output :
Height of the Node 25 is : 3

You may also like...

  • sunny kansal

    There’s some­thing not cor­rect. Height of Node x => 25 should be 1 not 3. Can you check it please?

    • tuto­ri­al­hori­zon

      We are con­sid­er­ing the height here as num­ber of nodes above the par­tic­u­lar node (includ­ing the node itself)

      • sunny kansal

        Still out­put and solu­tion 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 num­ber of lev­els are 3…

  • are­fin nowshad

    This code seems like cal­cu­lat­ing depth of a node. Even that is actu­ally wrong, depth should be 2 for Node 25 and height is 1. You cant con­sider the height as the num­ber of nodes above the par­tic­u­lar node includ­ing the node itself as the right way to define height of a node. Because this is out­right wrong.
    http://stackoverflow.com/questions/2603692/what-is-the-difference-between-tree-depth-and-height