Beautiful Vertices - Wrong Answer

Getting wrong answer. Please help with this

Hi Koshima

I’m not getting why you are using Stack here, if you simply think over this problem then you just have to count all those nodes who has more children than it’s parent has. So, for this you can just create a node which will be storing all this information as shown below:

 class node{
        boolean visited = false;
        int myChildren = 0;
        node parent = null;
        ArrayList<node> adj = new ArrayList<node>();
        
        public void addNode(node a){
            adj.add(a);
        }   
   }

(I guess this class is self explanatory, if you find any difficulty in understanding this then do let me know.)

After this, you just have to visit all the nodes and store the info, then you can just put a loop and check
if(node[i].myChildren > node[i].parent.myChildren) then increase the count of beautiful vertices.

I hope this will help you, if you have any further query then please feel free to respond to this thread.

hi @sanjeetboora
I have used stack for dfs traversal. Can you please explain the use of ArrayList adj? Can’t it be implemented without this ArrayList? I have tried without it. Please check.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.