
- 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 path from root to a given node in a binary tree using C++
In this tutorial, we will be discussing a program to print the path from root to a given node in a binary tree.
For a given binary tree having distinct nodes, we have to print the complete path to reach a particularly given node from the root node of the binary tree.
To solve this problem, we will use recursion. While traversing the binary tree, we will recursively search for the particular element to be found. Also alongside we will be storing the path to reach the element to be searched.
Example
#include <bits/stdc++.h> using namespace std; struct Node{ int data; Node *left, *right; }; struct Node* create_node(int data){ struct Node *new_node = new Node; new_node->data = data; new_node->left = new_node->right = NULL; return new_node; } //checks if a path from root node to element exists bool is_path(Node *root, vector<int>& arr, int x){ if (!root) return false; arr.push_back(root->data); if (root->data == x) return true; if (is_path(root->left, arr, x) || is_path(root->right, arr, x)) return true; arr.pop_back(); return false; } //printing the path from the root node to the element void print_path(Node *root, int x){ vector<int> arr; if (is_path(root, arr, x)){ for (int i=0; i<arr.size()-1; i++) cout << arr[i] << " -> "; cout << arr[arr.size() - 1]; } else cout << "Path doesn't exists" << endl; } int main(){ struct Node *root = create_node(13); root->left = create_node(21); root->right = create_node(43); root->left->left = create_node(34); root->left->right = create_node(55); root->right->left = create_node(68); root->right->right = create_node(79); int x = 68; print_path(root, x); return 0; }
Output
13 -> 43 -> 68
- Related Articles
- Program to print path from root to all nodes in a Complete Binary Tree using C++
- Program to print the first shortest root to leaf path in a Binary Tree using C++
- Find distance from root to given node in a binary tree in C++
- Program to print the longest leaf to leaf path in a Binary tree using C++
- Print Ancestors of a given node in Binary Tree in C++
- Print the first shortest root to leaf path in a Binary Tree in C++ Programming.
- Print ancestors of a given binary tree node without recursion in C++
- Program to find sum of longest sum path from root to leaf of a binary tree in Python
- Program to print nodes between two given level numbers of a binary tree using C++
- Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree in C++
- C++ program to Replace a Node with Depth in a Binary Tree
- Program to change the root of a binary tree using Python
- Program to pruning a given binary tree in C++
- Print cousins of a given node in Binary Treein C++
- Create a mirror tree from the given binary tree in C++ Program

Advertisements