Calculate depth of a full Binary tree from Preorder
Last Updated : 23 Jul, 2025
Given the preorder sequence of a full binary tree, calculate its depth(or height) [starting from depth 0]. The preorder is given as a string with two possible characters.
'l' denotes the leaf node
'n' denotes internal node
The given tree can be seen as a full binary tree where every node has 0 or two children. The two children of a node can be 'n' or a or a mix of both.
Examples :
Input: s = "nlnll" Output: 2 Explanation: Below is the representation of the tree formed from the string with depth equals to 2.
Input: s = "nlnnlll" Output: 3 Explanation: Below is the representation of the tree formed from the string with depth equals to 3.
Approach:
The idea is to traverse the binary tree using preorder traversal recursively using the given preorder sequence. If the index at current node is out of bounds or the node is equal to 'l', then return 0. Otherwise, recursively traverse the left subtree and right subtree, and return 1 + max(left, right) as output.
Below is the implementation of the above approach:
C++
// C++ program to find height of full binary tree// using preorder#include<bits/stdc++.h>usingnamespacestd;// function to return max of left subtree height// or right subtree heightintfindDepthRec(string&s,intn,int&index){if(index>=n||s[index]=='l')return0;// calc height of left subtree (In preorder// left subtree is processed before right)index++;intleft=findDepthRec(s,n,index);// calc height of right subtreeindex++;intright=findDepthRec(s,n,index);returnmax(left,right)+1;}intfindDepth(string&s,intn){intindex=0;returnfindDepthRec(s,n,index);}intmain(){strings="nlnnlll";cout<<findDepth(s,s.size())<<endl;return0;}
Java
// Java program to find height // of full binary tree using// preorderimportjava.io.*;classGfG{// function to return max // of left subtree height// or right subtree heightstaticintfindDepthRec(Strings,intn,int[]index){if(index[0]>=n||s.charAt(index[0])=='l')return0;// calc height of left subtree // (In preorder left subtree // is processed before right)index[0]++;intleft=findDepthRec(s,n,index);// calc height of// right subtreeindex[0]++;intright=findDepthRec(s,n,index);returnMath.max(left,right)+1;}staticintfindDepth(Strings,intn){// index is represented as array to // pass it by referenceint[]index={0};return(findDepthRec(s,n,index));}publicstaticvoidmain(String[]args){Strings="nlnnlll";intn=s.length();System.out.println(findDepth(s,n));}}
Python
#Python program to find height of full binary tree # using preorder# function to return max of left subtree height # or right subtree height deffindDepthRec(s,n,index):if(index[0]>=nors[index[0]]=='l'):return0# calc height of left subtree (In preorder # left subtree is processed before right) index[0]+=1left=findDepthRec(s,n,index)# calc height of right subtree index[0]+=1right=findDepthRec(s,n,index)return(max(left,right)+1)deffindDepth(s,n):# index is represented as array to # pass it by referenceindex=[0]returnfindDepthRec(s,n,index)if__name__=='__main__':s="nlnnlll"n=len(s)print(findDepth(s,n))
C#
// C# program to find height of// full binary tree using preorderusingSystem;classGfG{// function to return max of left subtree height// or right subtree heightstaticintFindDepthRec(char[]tree,intn,refintindex){if(index>=n||tree[index]=='l')return0;// calc height of left subtree (In preorder// left subtree is processed before right)index++;intleft=FindDepthRec(tree,n,refindex);// calc height of right subtreeindex++;intright=FindDepthRec(tree,n,refindex);returnMath.Max(left,right)+1;}staticintFindDepth(char[]tree,intn){intindex=0;returnFindDepthRec(tree,n,refindex);}staticvoidMain(){char[]tree="nlnnlll".ToCharArray();intn=tree.Length;Console.WriteLine(FindDepth(tree,n));}}
JavaScript
// Javascript program to find height of// full binary tree using preorder// function to return max of left subtree height// or right subtree heightfunctionfindDepthRec(tree,n,index){if(index[0]>=n||tree[index[0]]==='l')return0;// calc height of left subtree (In preorder// left subtree is processed before right)index[0]++;letleft=findDepthRec(tree,n,index);// calc height of right subtreeindex[0]++;letright=findDepthRec(tree,n,index);returnMath.max(left,right)+1;}functionfindDepth(tree){// index is represented as array to // pass it by referenceletindex=[0];returnfindDepthRec(tree,tree.length,index);}consttree="nlnnlll";console.log(findDepth(tree));
Output
3
Time Complexity: O(n) ,where n is the size of array. Auxiliary Space: O(h)