- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Find Deepest Left Leaf in a Binary Tree
A binary tree which has at most two children, specified as left child and right child. This is a C++ Program to Find Deepest Left Leaf in a Binary Tree
Algorithm
Begin. function deepestLLeafutil() find the deepest left leaf in a given binary tree: lvel is level of current node. maxlvel is pointer to the deepest left leaf node found so far isLeft Indicates that this node is left child of its parent resPtr is Pointer to the result If root is equal to Null then Return. Update result if this node is having a left leaf and its level is more than the max level of the current result. Recursively call function deepestLLeafutil() for left and right subtrees. End.
Example Code
#include <iostream> using namespace std; struct n { int v; n *l, *r; }; void deepestLLeafutil(n *root, int lvel, int *maxvel, bool isLeft, n **resPtr) { if (root == NULL) return; if (isLeft && !root->l && !root->r && lvel > *maxvel) { *resPtr = root; *maxvel = lvel; return; } deepestLLeafutil(root->l, lvel + 1, maxvel, true, resPtr); deepestLLeafutil(root->r, lvel + 1, maxvel, false, resPtr); } n* deepestLLeaf( n *root) { int maxlevel = 0; n *res = NULL; deepestLLeafutil(root, 0, &maxlevel, false, &res); return res; } n *newnode(int d) { n *t = new n; t->v = d; t->l = t->r = NULL; return t; } int main() { n* root = newnode(9); root->l = newnode(7); root->r = newnode(10); root->l->l = newnode(6); root->r->l= newnode(8); root->r->r = newnode(19); root->r->l->r = newnode(4); root->r->r->r = newnode(20); n *res = deepestLLeaf(root); if (res) cout << "The deepest left leaf is " << res->v; else cout << "There is no left leaf in the given tree"; return 0; }
Output
The deepest left leaf is 6
- Related Articles
- Deepest left leaf node in a binary tree in C++
- Program to find second deepest node in a binary tree in python
- Find the Deepest Node in a Binary Tree in C++
- Find the sum of left leaf nodes of a given Binary Tree in C++
- Program to find leaf and non-leaf nodes of a binary tree in Python
- Program to print the longest leaf to leaf path in a Binary tree using 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++
- Program to find the left side view of a binary tree in C++
- Depth of the deepest odd level node in Binary Tree in C++ Program
- Print leaf nodes in binary tree from left to right using one stack in C++
- Program to find leftmost deepest node of a tree in Python
- Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- C++ Program to Perform Left Rotation on a Binary Search Tree

Advertisements