+ 4

Count the number of leaf nodes.

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 tell the number of leaf nodes in a tree after deleting the given node.Note that deleting a node deletes all the nodes in its subtree.

7th Apr 2018, 6:56 AM
sitesh Kumar behera
sitesh Kumar behera - avatar
4 Answers
0
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<int> v[nodes+1];
7th Apr 2018, 7:16 AM
Bartosz Pieszko
Bartosz Pieszko - avatar
0
plz give me java code
8th Apr 2018, 5:47 PM
Digambar Modi
0
yes it is correct..
1st May 2018, 4:21 PM
Ashish Chakraborty
Ashish Chakraborty - avatar
0
Java code..?
2nd May 2018, 4:44 AM
Prathap Shet
Prathap Shet - avatar