Calculate depth of a full Binary tree from Preorder
Last Updated :
11 Sep, 2023
Given preorder of a 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
- ‘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 ‘n’ or ‘l’ or mix of both.
Examples :
Input : nlnll
Output : 2
Explanation :
Input : nlnnlll
Output : 3
Preorder of the binary tree is given so traverse
Also, we would be given a string of char (formed of ‘n’ and ‘l’), so there is no need to implement tree also.
The recursion function would be:
- Base Case: return 0; when tree[i] = ‘l’ or i >= strlen(tree)
- find_depth( tree[i++] ) //left subtree
- find_depth( tree[i++] ) //right subtree
Where i is the index of the string tree.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findDepthRec( char tree[], int n, int & index)
{
if (index >= n || tree[index] == 'l' )
return 0;
index++;
int left = findDepthRec(tree, n, index);
index++;
int right = findDepthRec(tree, n, index);
return max(left, right) + 1;
}
int findDepth( char tree[], int n)
{
int index = 0;
return findDepthRec(tree, n, index);
}
int main()
{
char tree[] = "nlnnlll" ;
int n = strlen (tree);
cout << findDepth(tree, n) << endl;
return 0;
}
|
Java
import java .io.*;
class GFG
{
static int findDepthRec(String tree,
int n, int index)
{
if (index >= n ||
tree.charAt(index) == 'l' )
return 0 ;
index++;
int left = findDepthRec(tree,
n, index);
index++;
int right = findDepthRec(tree, n, index);
return Math.max(left, right) + 1 ;
}
static int findDepth(String tree,
int n)
{
int index = 0 ;
return (findDepthRec(tree,
n, index));
}
static public void main(String[] args)
{
String tree = "nlnnlll" ;
int n = tree.length();
System.out.println(findDepth(tree, n));
}
}
|
Python3
def findDepthRec(tree, n, index) :
if (index[ 0 ] > = n or tree[index[ 0 ]] = = 'l' ):
return 0
index[ 0 ] + = 1
left = findDepthRec(tree, n, index)
index[ 0 ] + = 1
right = findDepthRec(tree, n, index)
return ( max (left, right) + 1 )
def findDepth(tree, n) :
index = [ 0 ]
return findDepthRec(tree, n, index)
if __name__ = = '__main__' :
tree = "nlnnlll"
n = len (tree)
print (findDepth(tree, n))
|
C#
using System;
class GFG {
static int findDepthRec( char [] tree, int n, int index)
{
if (index >= n || tree[index] == 'l' )
return 0;
index++;
int left = findDepthRec(tree, n, index);
index++;
int right = findDepthRec(tree, n, index);
return Math.Max(left, right) + 1;
}
static int findDepth( char [] tree, int n)
{
int index = 0;
return (findDepthRec(tree, n, index));
}
static public void Main()
{
char [] tree = "nlnnlll" .ToCharArray();
int n = tree.Length;
Console.WriteLine(findDepth(tree, n));
}
}
|
Javascript
<script>
function findDepthRec(tree, n, index)
{
if (index >= n || tree[index] == 'l' )
return 0;
index++;
let left = findDepthRec(tree, n, index);
index++;
let right = findDepthRec(tree, n, index);
return Math.max(left, right) + 1;
}
function findDepth(tree, n)
{
let index = 0;
return (findDepthRec(tree, n, index));
}
let tree = "nlnnlll" .split( '' );
let n = tree.length;
document.write(findDepth(tree, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...