
- 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
Deepest left leaf node in a binary tree in C++
In this tutorial, we are going to find the deepest left leaf node in the binary tree. Let's see the binary tree.
A B C D E F G
Let's see the steps to solve the problem.
Write a Node struct with char, left, and right pointers.
Initialize the binary tree with dummy data.
Write a recursive function to find the deepest left node in the binary function. It takes three argument root node, isLeftNode, and result pointer to store the deepest node.
If the current node is left and is leaf node, then update the result node with current node.
Call the recursive function on left sub tree.
Call the recursive function on right sub tree.
If the result node is null, then there is no node that satisfies our conditions.
Else print the data in the result node.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; struct Node { char data; struct Node *left, *right; }; Node *addNewNode(char data) { Node *newNode = new Node; newNode->data = data; newNode->left = newNode->right = NULL; return newNode; } void getDeepestLeftLeafNode(Node *root, bool isLeftNode, Node **resultPointer) { if (root == NULL) { return; } if (isLeftNode && !root->left && !root->right) { *resultPointer = root; return; } getDeepestLeftLeafNode(root->left, true, resultPointer); getDeepestLeftLeafNode(root->right, false, resultPointer); } int main() { Node* root = addNewNode('A'); root->left = addNewNode('B'); root->right = addNewNode('C'); root->left->left = addNewNode('D'); root->right->left = addNewNode('E'); root->right->right = addNewNode('F'); root->right->left->right = addNewNode('G'); Node *result = NULL; getDeepestLeftLeafNode(root, false, &result); if (result) { cout << "The deepest left child is " << result->data << endl; } else { cout << "There is no left leaf in the given tree" << endl; } return 0; }
Output
If you execute the above program, then you will get the following result.
The deepest left child is D
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- C++ Program to Find Deepest Left Leaf in a Binary Tree
- Find the Deepest Node in a Binary Tree in C++
- Depth of the deepest odd level node in Binary Tree in C++?
- Program to find second deepest node in a binary tree in python
- Depth of the deepest odd level node in Binary Tree in C++ Program
- Print all leaf nodes of a binary tree from right to left in C++
- Find the sum of left leaf nodes of a given Binary Tree in C++
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Program to find leftmost deepest node of a tree in Python
- Find the closest leaf in a Binary Tree in C++
- Count Non-Leaf nodes in a Binary Tree in C++
- Second Minimum Node In a Binary Tree in C++
- Print leaf nodes in binary tree from left to right using one stack in C++
- Preorder Successor of a Node in Binary Tree in C++
