

- 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
Program to print the first shortest root to leaf path in a Binary Tree using C++
In this tutorial, we will be discussing a program to print the first shortest root to leaf path in a Binary Tree.
In this, we will be given a binary tree with distinct values, and we have to find the shortest path from the root node to the leaf node in a given binary tree.
To solve this, we can use a queue to perform level order traversal in a binary tree and store the nodes in the shortest path. For this, once we reach the leftmost node for the final level, we retrieve the values from the queue and print the shortest path.
Example
#include <bits/stdc++.h> using namespace std; struct Node{ struct Node* left; struct Node* right; int data; }; struct Node* create_node(int data){ struct Node* temp = new Node; temp->data = data; temp->left = NULL; temp->right = NULL; return temp; } void print_spath(int Data, unordered_map<int, int> parent){ if (parent[Data] == Data) return; print_spath(parent[Data], parent); cout << parent[Data] << " "; } void leftmost_path(struct Node* root){ queue<struct Node*> q; q.push(root); int LeafData = -1; struct Node* temp = NULL; unordered_map<int, int> parent; parent[root->data] = root->data; while (!q.empty()){ temp = q.front(); q.pop(); if (!temp->left && !temp->right){ LeafData = temp->data; break; } else{ if (temp->left){ q.push(temp->left); parent[temp->left->data] = temp->data; } if (temp->right) { q.push(temp->right); parent[temp->right->data] = temp->data; } } } print_spath(LeafData, parent); cout << LeafData << " "; } int main(){ struct Node* root = create_node(21); root->left = create_node(24); root->right = create_node(35); root->left->left = create_node(44); root->right->left = create_node(53); root->right->right = create_node(71); root->left->left->left = create_node(110); root->left->left->right = create_node(91); root->right->right->left = create_node(85); leftmost_path(root); return 0; }
Output
21 35 53
- Related Questions & Answers
- Print the first shortest root to leaf path in a Binary Tree in C++ Programming.
- Program to print the longest leaf to leaf path in a Binary tree using C++
- Program to print path from root to a given node in a binary tree using C++
- Program to print path from root to all nodes in a Complete Binary Tree using C++
- Program to find sum of longest sum path from root to leaf of a binary tree in Python
- Program to print root to leaf paths without using recursion using C++
- Print shortest path to print a string on screen in C Program.
- Program to change the root of a binary tree using Python
- Print root to leaf paths without using recursion in C++ Programming.
- 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++
- Shortest Path in Binary Matrix in C++
- Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++
- Program to find leaf and non-leaf nodes of a binary tree in Python
Advertisements