Home » Data Structure & Algorithms » Height or Depth of Binary Tree

Height or Depth of Binary Tree

Question :
WAP to program to find the height or depth of binary tree in O(n) time complexity.

 

Solution :
The problem is solved in recursive way.

Algorithm :
Step 1 : If node is NULL then return 0.
Step 2 : Recursively find left height or depth of tree;
Step 3 : Recursively find right height or depth of tree;
Step 4 : Return the 1+maximum between Step 2 & Step 3.

Program :

 


Leave a comment

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

20 + 5 =