We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Nowhere in the problem it is stated that one of the two nodes of an edge is 100% the parent. Which means, you cannot assume it.
You can work yourself through construction level by level, with a queue. Dequeue the next potential parent, and enqueue all implemented children. Save all edges in an adjacent list, then implement this strategy.
Mark all visited nodes! You don't want to accidentally attatch a parent as another child.
Modulo needs clarification. You are expected to use it on every calculation for every node, and not on the final sum.
This is my solution:
classSumInLeavesVisitorextendsTreeVis{privateintsumInLeavesVisitor=0;publicintgetResult(){//implement thisreturnsumInLeavesVisitor;}publicvoidvisitNode(TreeNodenode){//implement this}publicvoidvisitLeaf(TreeLeafleaf){//implement thissumInLeavesVisitor+=leaf.getValue();}}classProductOfRedNodesVisitorextendsTreeVis{privateintproductOfRedNodesVisitor=1;finalprivateintmod=1000000007;publicintgetResult(){//implement thisreturnproductOfRedNodesVisitor;}publicvoidvisitNode(TreeNodenode){//implement thisif(node.getColor()==Color.RED)productOfRedNodesVisitor=(int)(((long)productOfRedNodesVisitor*node.getValue())%mod);}publicvoidvisitLeaf(TreeLeafleaf){//implement thisif(leaf.getColor()==Color.RED)productOfRedNodesVisitor=(int)(((long)productOfRedNodesVisitor*leaf.getValue())%mod);}}classFancyVisitorextendsTreeVis{privateintnonLeafValue=0;privateintleafValue=0;publicintgetResult(){//implement thisintdifference=leafValue-nonLeafValue;returnMath.abs(difference);}publicvoidvisitNode(TreeNodenode){//implement thisif(node.getDepth()%2==0)nonLeafValue+=node.getValue();}publicvoidvisitLeaf(TreeLeafleaf){//implement thisif(leaf.getColor()==Color.GREEN)leafValue+=leaf.getValue();}}publicclassSolution{publicstaticTreesolve(){//read the tree from STDIN and return its root as a return value of this functionScannersc=newScanner(System.in);intn=sc.nextInt();int[]nodeValues=newint[n];Color[]nodeColors=newColor[n];List<List<Integer>>adjList=newArrayList<>();for(inti=0;i<n;i++){nodeValues[i]=sc.nextInt();adjList.add(newArrayList<Integer>());}for(inti=0;i<n;i++){nodeColors[i]=sc.nextInt()==0?Color.RED:Color.GREEN;}Tree[]treeNodes=newTree[n];treeNodes[0]=newTreeNode(nodeValues[0],nodeColors[0],0);Queue<Integer>parentQueue=newLinkedList<>();parentQueue.add(0);boolean[]visited=newboolean[n];visited[0]=true;for(inti=0;i<n-1;i++){intu=sc.nextInt()-1;intv=sc.nextInt()-1;adjList.get(u).add(v);adjList.get(v).add(u);}while(!parentQueue.isEmpty()){intparentIndex=parentQueue.poll();Treeparent=treeNodes[parentIndex];for(intchildIndex:adjList.get(parentIndex)){if(!visited[childIndex]){Treechild;if(adjList.get(childIndex).size()==1){child=newTreeLeaf(nodeValues[childIndex],nodeColors[childIndex],parent.getDepth()+1);}else{child=newTreeNode(nodeValues[childIndex],nodeColors[childIndex],parent.getDepth()+1);}treeNodes[childIndex]=child;visited[childIndex]=true;parentQueue.add(childIndex);((TreeNode)parent).addChild(child);}}}returntreeNodes[0];}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Java Visitor Pattern
You are viewing a single comment's thread. Return to all comments →
This is my take on this problem:
Nowhere in the problem it is stated that one of the two nodes of an edge is 100% the parent. Which means, you cannot assume it.
You can work yourself through construction level by level, with a queue. Dequeue the next potential parent, and enqueue all implemented children. Save all edges in an adjacent list, then implement this strategy.
Mark all visited nodes! You don't want to accidentally attatch a parent as another child.
Modulo needs clarification. You are expected to use it on every calculation for every node, and not on the final sum.
This is my solution: