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:

public class NodeHeight {
public int getNodeHeight(Node root, Node x, int height){
if(root==null) return 0;
if(root==x) return height;
//check if the node is present in the left sub tree
int level = getNodeHeight(root.left,x,height+1);
//System.out.println(level);
if(level!=0) return level;
//check if the node is present in the right sub tree
return getNodeHeight(root.right,x,height+1);
}
public static void main (String[] args) throws java.lang.Exception
{
Node root = new Node(5);
root.left = new Node(10);
root.right = new Node(15);
root.left.left = new Node(20);
Node x = new Node(25);
root.left.right = x;
root.left.right.left = new Node(35);
NodeHeight i = new NodeHeight();
System.out.println("Height of the Node " + x.data + " is : " + i.getNodeHeight(root,x,1));
}
}
class Node{
int data;
Node left;
Node right;
public Node(int data){
this.data = data;
this.left = null;
this.right = null;
}
}

view raw
NodeHeight.java
hosted with ❤ by GitHub

Output :
Height of the Node 25 is : 3