Find height of a special binary tree whose leaf nodes are connected
Last Updated :
05 Jul, 2023
Given a special binary tree whose leaf nodes are connected to form a circular doubly linked list, find its height.
For example,
1
/ \
2 3
/ \
4 5
/
6
In the above binary tree, 6, 5 and 3 are leaf nodes and they form a circular doubly linked list. Here, the left pointer of leaf node will act as a previous pointer of circular doubly linked list and its right pointer will act as next pointer of circular doubly linked list.
The idea is to follow similar approach as we do for finding height of a normal binary tree. We recursively calculate height of left and right subtrees of a node and assign height to the node as max of the heights of two children plus 1. But left and right child of a leaf node are null for normal binary trees. But, here leaf node is a circular doubly linked list node. So for a node to be a leaf node, we check if node’s left’s right is pointing to the node and its right’s left is also pointing to the node itself.
Below is the implementation of above idea –
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
bool isLeaf(Node* node)
{
return node->left && node->left->right == node
&& node->right && node->right->left == node;
}
int maxDepth(Node* node)
{
if (node == NULL)
return 0;
if (isLeaf(node))
return 1;
return 1
+ max(maxDepth(node->left),
maxDepth(node->right));
}
Node* newNode( int data)
{
Node* node = new Node;
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->left->left->left = newNode(6);
Node* L1 = root->left->left->left;
Node* L2 = root->left->right;
Node* L3 = root->right;
L1->right = L2, L2->right = L3, L3->right = L1;
L3->left = L2, L2->left = L1, L1->left = L3;
cout << "Height of tree is " << maxDepth(root);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Node {
int data;
Node left, right;
Node( int key)
{
data = key;
left = right = null ;
}
}
class GFG {
static boolean isLeaf(Node node)
{
return (node.left != null && node.left.right == node
&& node.right != null
&& node.right.left == node);
}
static int maxDepth(Node node)
{
if (node == null )
return 0 ;
if (isLeaf(node))
return 1 ;
return 1
+ Math.max(maxDepth(node.left),
maxDepth(node.right));
}
public static void main(String args[])
{
Node root = new Node( 1 );
root.left = new Node( 2 );
root.right = new Node( 3 );
root.left.left = new Node( 4 );
root.left.right = new Node( 5 );
root.left.left.left = new Node( 6 );
Node L1 = root.left.left.left;
Node L2 = root.left.right;
Node L3 = root.right;
L1.right = L2;
L2.right = L3;
L3.right = L1;
L3.left = L2;
L2.left = L1;
L1.left = L3;
System.out.println( "Height of tree is "
+ maxDepth(root));
}
}
|
Python3
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def isLeaf(node):
return node.left and node.left.right = = node and \
node.right and node.right.left = = node
def maxDepth(node):
if (node = = None ):
return 0
if (isLeaf(node)):
return 1
return 1 + max (maxDepth(node.left), maxDepth(node.right))
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
root.left.left.left = newNode( 6 )
L1 = root.left.left.left
L2 = root.left.right
L3 = root.right
L1.right = L2
L2.right = L3
L3.right = L1
L3.left = L2
L2.left = L1
L1.left = L3
print ( "Height of tree is " , maxDepth(root))
|
C#
using System;
public class Node {
public int data;
public Node left, right;
public Node( int key)
{
data = key;
left = right = null ;
}
}
public class GFG {
static bool isLeaf(Node node)
{
return (node.left != null && node.left.right == node
&& node.right != null
&& node.right.left == node);
}
static int maxDepth(Node node)
{
if (node == null )
return 0;
if (isLeaf(node))
return 1;
return 1
+ Math.Max(maxDepth(node.left),
maxDepth(node.right));
}
public static void Main(String[] args)
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.left.left.left = new Node(6);
Node L1 = root.left.left.left;
Node L2 = root.left.right;
Node L3 = root.right;
L1.right = L2;
L2.right = L3;
L3.right = L1;
L3.left = L2;
L2.left = L1;
L1.left = L3;
Console.WriteLine( "Height of tree is "
+ maxDepth(root));
}
}
|
Javascript
<script>
class Node
{
constructor(key)
{
this .data = key;
this .left = this .right = null ;
}
}
function isLeaf(node)
{
return (node.left != null && node.left.right == node
&& node.right != null
&& node.right.left == node);
}
function maxDepth(node)
{
if (node == null )
return 0;
if (isLeaf(node))
return 1;
return 1
+ Math.max(maxDepth(node.left),
maxDepth(node.right));
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.left.left.left = new Node(6);
let L1 = root.left.left.left;
let L2 = root.left.right;
let L3 = root.right;
L1.right = L2;
L2.right = L3;
L3.right = L1;
L3.left = L2;
L2.left = L1;
L1.left = L3;
document.write( "Height of tree is "
+ maxDepth(root));
</script>
|
Output
Height of tree is 4
Time Complexity: O(N), where N is the number of nodes.
Auxiliary space: O(N), if it is skewed tree.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...