
- 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
Print root to leaf paths without using recursion in C++ Programming.
Given the binary tree the program must find out the multiple paths from the root to a leaf which means all the paths should be printed but the challenge is to without using recursion.
We will traverse the tree iteratively as the constraint is to do it without recursion. So to achieve this we can use an STL map that will store the root element and whenever the leaf node is identified through level order traversal it will print the path from root to leaf as there is a map pointer which is pointing to a root node.
In the above tree, there are multiple paths which can be generated for reaching from root to leaf −
10 -> 3 -> 140 10 -> 3 -> 162 10 -> 211 -> 100 10 -> 211 -> 146
Hence, the program must print all the given paths as an output of the given binary tree.
Algorithm
START Step 1 -> create a structure of a node as struct Node struct node *left, *right int data End Step 2 -> function to create a node node* newnode(int data) node->data = data node->left = node->right = NULL; return (node) Step 3 -> create function to calculate the path void calculatePath(Node* curr, map<Node*, Node*> first) create STL stack<Node*> stk Loop While (curr) stk.push(curr) curr = first[curr] End Loop While !stk.empty() curr = stk.top() stk.pop() print curr->data End Step 4 -> create function to find the leaf nodes void leaf(Node* root) IF root = NULL Return End Create STL stack<Node*> stc stc.push(root) Create STL map<Node*, Node*> prnt prnt[root] = NULL Loop while !stc.empty() Node* curr = stc.top() stc.pop() IF!(curr->left) && !(curr->right) calculatePath(curr, prnt) End IF curr->right prnt[curr->right] = curr stc.push(curr->right) End IF curr->left prnt[curr->left] = curr stc.push(curr->left) End End STOP
Example
#include <bits/stdc++.h> using namespace std; //structure of a node struct Node{ int data; struct Node *left, *right; }; //function to create a new node Node* newNode(int data){ Node* node = new Node; node->data = data; node->left = node->right = NULL; return node; } //this function will calculate the path void calculatePath(Node* curr, map<Node*, Node*> first){ stack<Node*> stk; while (curr){ stk.push(curr); curr = first[curr]; } while (!stk.empty()){ curr = stk.top(); stk.pop(); cout << curr->data << " "; } cout << endl; } //this function will lead to the leafs void leaf(Node* root){ if (root == NULL) return; stack<Node*> stc; stc.push(root); map<Node*, Node*> prnt; prnt[root] = NULL; while (!stc.empty()){ Node* curr = stc.top(); stc.pop(); if (!(curr->left) && !(curr->right)) calculatePath(curr, prnt); if (curr->right){ prnt[curr->right] = curr; stc.push(curr->right); } if (curr->left){ prnt[curr->left] = curr; stc.push(curr->left); } } } int main(){ Node* root = newNode(67); //it will insert the nodes to create a tree root->left = newNode(34); root->right = newNode(89); root->left->left = newNode(23); root->left->right = newNode(95); root->right->left = newNode(12); leaf(root); //call the function leaf return 0; }
Output
if we run the above program then it will generate the following output
67 34 23 67 34 95 67 89 12
- Related Articles
- Program to print root to leaf paths without using recursion using C++
- Print all root to leaf paths with there relative positions in C++
- Print the first shortest root to leaf path in a Binary Tree in C++ Programming.
- Insufficient Nodes in Root to Leaf Paths in Python
- C++ Remove Nodes on Root to Leaf Paths of Length < K
- Program to print the first shortest root to leaf path in a Binary Tree using C++
- Print 1 to 100 in C++, without loop and recursion
- Print a character n times without using loop, recursion or goto in C++
- Print a number 100 times without using loop, recursion and macro expansion in C
- Program to print the longest leaf to leaf path in a Binary tree using C++
- Python Program to Print the Alternate Nodes in a Linked List without using Recursion
- Write a C program to print ‘ABCD’ repeatedly without using loop, recursion and any control structure
- Print all the paths from root, with a specified sum in Binary tree in C++
- Print ancestors of a given binary tree node without recursion in C++
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
