- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find height of a special binary tree whose leaf nodes are connected in C++

Suppose we have a special binary tree, whose leaf nodes are connected to form a circular doubly linked list. We have to find its height. So the left pointer of the left most leaf will act as previous pointer of circular doubly linked list, and its right pointer will act as next pointer of the linked list.

In this case the height finding strategy is similar to the normal binary search tree. We recursively calculate the height of the left and right subtrees of a node and assign height to the node is max of the two children + 1. But here leaves are elements of circular doubly linked list. So for a node to be leaf node we check if the nodes left’s right is pointing to the node and its right’s left is pointing to the node itself.

## Example

#include<iostream> using namespace std; class Node { public: int data; Node *left, *right; }; bool isLeafNode(Node* node) { return node->left && node->left->right == node && node->right && node->right->left == node; } int findHeight(Node* node) { if (node == NULL) return 0; if (isLeafNode(node)) return 1; return 1 + max(findHeight(node->left), findHeight(node->right)); } Node* getNode(int data) { Node* node = new Node; node->data = data; node->left = NULL; node->right = NULL; return node; } int main() { Node* root = getNode(1); root->left = getNode(2); root->right = getNode(3); root->left->left = getNode(4); root->left->right = getNode(5); root->left->left->left = getNode(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: " << findHeight(root); }

## Output

Height of tree is: 4

- Related Questions & Answers
- Program to find leaf and non-leaf nodes of a binary tree in Python
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Find the sum of left leaf nodes of a given Binary Tree in C++
- Product of all leaf nodes of binary tree in C++
- Count Non-Leaf nodes in a Binary Tree in C++
- Print all leaf nodes of a binary tree from right to left in C++
- Find the closest leaf in a Binary Tree in C++
- Find distance between two nodes of a Binary Tree in C++
- C++ Program to Find Deepest Left Leaf in a Binary Tree
- Program to Find Out the Special Nodes in a Tree in Python
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
- Maximum sum of non-leaf nodes among all levels of the given binary tree in C++
- Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++
- Find distance between two nodes of a Binary Tree in C++ Program
- Find Height of Binary Tree represented by Parent array in C++

Advertisements