
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to print the longest leaf to leaf path in a Binary tree using C++
In this tutorial, we will be discussing a program to print the longest path that exists from a leaf node to another leaf node in a given binary tree.
In other words, we have to print all the nodes that appear in the diameter of the Binary tree. Here, diameter (or width) for a particular binary tree is defined as the number of nodes in the longest path from one end node to another.
To solve this, we calculate the diameter of the binary tree using the height function. Then we find the longest path in the left portion of the binary tree and the right portion. Then finally to print the nodes in the diameter we print the left portion nodes, the root node and then the right portion nodes.
Example
#include <bits/stdc++.h> using namespace std; struct Node { int data; Node *left, *right; }; struct Node* create_node(int data){ struct Node* node = new Node; node->data = data; node->left = node->right = NULL; return (node); } int tree_height(Node* root, int& ans, Node*(&k), int& lh, int& rh, int& f){ if (root == NULL) return 0; int left_tree_height = tree_height(root->left, ans, k, lh, rh, f); int right_tree_height = tree_height(root->right, ans, k, lh, rh, f); if (ans < 1 + left_tree_height + right_tree_height){ ans = 1 + left_tree_height + right_tree_height; k = root; lh = left_tree_height; rh = right_tree_height; } return 1 + max(left_tree_height, right_tree_height); } void print_roottonode(int ints[], int len, int f){ int i; if (f == 0){ for (i = len - 1; i >= 0; i--) { printf("%d ", ints[i]); } } else if (f == 1) { for (i = 0; i < len; i++) { printf("%d ", ints[i]); } } } void print_pathr(Node* node, int path[], int pathLen, int max, int& f){ if (node == NULL) return; path[pathLen] = node->data; pathLen++; if (node->left == NULL && node->right == NULL) { if (pathLen == max && (f == 0 || f == 1)) { print_roottonode(path, pathLen, f); f = 2; } } else { print_pathr(node->left, path, pathLen, max, f); print_pathr(node->right, path, pathLen, max, f); } } void calc_diameter(Node* root){ if (root == NULL) return; int ans = INT_MIN, lh = 0, rh = 0; int f = 0; Node* k; int tree_height_of_tree = tree_height(root, ans, k, lh, rh, f); int lPath[100], pathlen = 0; print_pathr(k->left, lPath, pathlen, lh, f); printf("%d ", k->data); int rPath[100]; f = 1; print_pathr(k->right, rPath, pathlen, rh, f); } int main(){ struct Node* root = create_node(12); root->left = create_node(22); root->right = create_node(33); root->left->left = create_node(45); root->left->right = create_node(57); root->left->right->left = create_node(26); root->left->right->right = create_node(76); root->left->left->right = create_node(84); root->left->left->right->left = create_node(97); calc_diameter(root); return 0; }
Output
97 84 45 22 57 26
- Related Articles
- Program to print the first shortest root to leaf path in a Binary Tree using C++
- Program to find sum of longest sum path from root to leaf of a binary tree in Python
- Print the first shortest root to leaf path in a Binary Tree in C++ Programming.
- Program to find leaf and non-leaf nodes of a binary tree in Python
- C++ Program to Find Deepest Left Leaf in a Binary Tree
- Print leaf nodes in binary tree from left to right using one stack in C++
- Print all leaf nodes of a binary tree from right to left in C++
- Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++
- Find the closest leaf in a Binary Tree in C++
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Program to print root to leaf paths without using recursion using C++
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
- Count Non-Leaf nodes in a Binary Tree in C++
- Deepest left leaf node in a binary tree in C++
- Program to print path from root to a given node in a binary tree using C++

Advertisements