Given a special binary tree whose leaf nodes are connected to form a circular doubly linked list, find its height.
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 –
Height of tree is 4
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Sum of all leaf nodes of binary tree
- Product of all leaf nodes of binary tree
- Count Non-Leaf nodes in a Binary Tree
- Print all leaf nodes of a binary tree from right to left
- Print all leaf nodes of a Binary Tree from left to right
- Print the nodes of binary tree as they become the leaf node
- Pairwise Swap leaf nodes in a binary tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Program to count leaf nodes in a binary tree
- Maximum sum of leaf nodes among all levels of the given binary tree
- Relationship between number of nodes and height of binary tree
- Height of a complete binary tree (or Heap) with N nodes
- Print leaf nodes in binary tree from left to right using one stack
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Print left and right leaf nodes separately in Binary Tree
- Iterative program to count leaf nodes in a Binary Tree
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Print the nodes of the Binary Tree whose height is a Prime number