Check out example codes for "Find the minimum difference between pairs in a simple path of tree C++". It will help you in understanding the concepts better.

Code Example 1

``````// C++ implementation
#include <bits/stdc++.h>
using namespace std;

// An utility function to add an edge in an
// undirected graph.
int x,
int y)
{
v[x].push_back(y);
v[y].push_back(x);
}

// A function to print the path between
// the given pair of nodes.
void printPath(vector<int> stack)
{
int i;
for (i = 0; i < (int)stack.size() - 1;
i++) {
cout << stack[i] << " -> ";
}
cout << stack[i];
}

// An utility function to do
// DFS of graph recursively
// from a given vertex x.
void DFS(vector<int> v[],
bool vis[],
int x,
int y,
vector<int> stack)
{
stack.push_back(x);
if (x == y) {

// print the path and return on
// reaching the destination node
printPath(stack);
return;
}
vis[x] = true;

// A flag variable to keep track
// if backtracking is taking place
int flag = 0;
if (!v[x].empty()) {
for (int j = 0; j < v[x].size(); j++) {
// if the node is not visited
if (vis[v[x][j]] == false) {
DFS(v, vis, v[x][j], y, stack);
flag = 1;
}
}
}
if (flag == 0) {

// If backtracking is taking
// place then pop
stack.pop_back();
}
}

// A utility function to initialise
// visited for the node and call
// DFS function for a given vertex x.
void DFSCall(int x,
int y,
vector<int> v[],
int n,
vector<int> stack)
{
// visited array
bool vis[n + 1];

memset(vis, false, sizeof(vis));

// DFS function call
DFS(v, vis, x, y, stack);
}

// Driver Code
int main()
{
int n = 10;
vector<int> v[n], stack;

// Vertex numbers should be from 1 to 9.