You are given a tree with n nodes numbered from 0 to n-1 . you are also given a node x which you are supposed to delete. you have to tell the number of leaf nodes in the tree after deleting the given node. note that deleting a node deletes all the nodes in its subtree.
Answers
Answered by
3
This is a question on programming.
We will use Java programming language.
Find the program as below:
int dfs (int node, int parent)
{
int res = 0
for (auto i : v [node] )
if ( i ! = deleted && i ! = parent )
res + = dfs ( i, node )
if ( v [ node ] . size () ==1 )
return 1 ;
else
return res ;
Start this function from root of the tree.
Vector v [ nodes + 1 ] ;
The above program when run will perform the required task.
We will use Java programming language.
Find the program as below:
int dfs (int node, int parent)
{
int res = 0
for (auto i : v [node] )
if ( i ! = deleted && i ! = parent )
res + = dfs ( i, node )
if ( v [ node ] . size () ==1 )
return 1 ;
else
return res ;
Start this function from root of the tree.
Vector v [ nodes + 1 ] ;
The above program when run will perform the required task.
Answered by
0
Explanation:
You are given a tree with n nodes numbered from 0 to n-1 . you are also given a node x which you are supposed to delete. you have to tell the number of leaf nodes in the tree
Similar questions