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.
Useful tutorial for Visitor Pattern However, this problem is more about creating a tree in an obscure format than it is about Visitor patterns.
Common pitfall: The edges of the tree in the provided input are undirected edges. This makes creating a directed and rooted tree challenging.
classSumInLeavesVisitorextendsTreeVis{privateintresult=0;publicintgetResult(){returnresult;}publicvoidvisitNode(TreeNodenode){// do nothing}publicvoidvisitLeaf(TreeLeafleaf){result+=leaf.getValue();}}classProductOfRedNodesVisitorextendsTreeVis{privatelongresult=1;privatefinalintM=1000000007;publicintgetResult(){return(int)result;}publicvoidvisitNode(TreeNodenode){if(node.getColor()==Color.RED){result=(result*node.getValue())%M;}}publicvoidvisitLeaf(TreeLeafleaf){if(leaf.getColor()==Color.RED){result=(result*leaf.getValue())%M;}}}classFancyVisitorextendsTreeVis{privateintnonLeafEvenDepthSum=0;privateintgreenLeavesSum=0;publicintgetResult(){returnMath.abs(nonLeafEvenDepthSum-greenLeavesSum);}publicvoidvisitNode(TreeNodenode){if(node.getDepth()%2==0){nonLeafEvenDepthSum+=node.getValue();}}publicvoidvisitLeaf(TreeLeafleaf){if(leaf.getColor()==Color.GREEN){greenLeavesSum+=leaf.getValue();}}}publicclassSolution{privatestaticint[]values;privatestaticColor[]colors;privatestaticHashMap<Integer,HashSet<Integer>>map;publicstaticTreesolve(){Scannerscan=newScanner(System.in);intnumNodes=scan.nextInt();/* Read and save nodes and colors */values=newint[numNodes];colors=newColor[numNodes];map=newHashMap<>(numNodes);for(inti=0;i<numNodes;i++){values[i]=scan.nextInt();}for(inti=0;i<numNodes;i++){colors[i]=scan.nextInt()==0?Color.RED:Color.GREEN;}/* Save edges */for(inti=0;i<numNodes-1;i++){intu=scan.nextInt();intv=scan.nextInt();/* Edges are undirected: Add 1st direction */HashSet<Integer>uNeighbors=map.get(u);if(uNeighbors==null){uNeighbors=newHashSet<>();map.put(u,uNeighbors);}uNeighbors.add(v);/* Edges are undirected: Add 2nd direction */HashSet<Integer>vNeighbors=map.get(v);if(vNeighbors==null){vNeighbors=newHashSet<>();map.put(v,vNeighbors);}vNeighbors.add(u);}scan.close();/* Handle 1-node tree */if(numNodes==1){returnnewTreeLeaf(values[0],colors[0],0);}/* Create Tree */TreeNoderoot=newTreeNode(values[0],colors[0],0);addChildren(root,1);returnroot;}/* Recursively adds children of a TreeNode */privatestaticvoidaddChildren(TreeNodeparent,IntegerparentNum){/* Get HashSet of children and loop through them */for(IntegertreeNum:map.get(parentNum)){map.get(treeNum).remove(parentNum);// removes the opposite arrow direction/* Add child */HashSet<Integer>grandChildren=map.get(treeNum);booleanchildHasChild=(grandChildren!=null&&!grandChildren.isEmpty());Treetree;if(childHasChild){tree=newTreeNode(values[treeNum-1],colors[treeNum-1],parent.getDepth()+1);}else{tree=newTreeLeaf(values[treeNum-1],colors[treeNum-1],parent.getDepth()+1);}parent.addChild(tree);/* Recurse if necessary */if(treeinstanceofTreeNode){addChildren((TreeNode)tree,treeNum);}}}
Java Visitor Pattern
You are viewing a single comment's thread. Return to all comments →
Java solution - passes 100% of test cases
From my HackerRank solutions.
Useful tutorial for Visitor Pattern However, this problem is more about creating a tree in an obscure format than it is about Visitor patterns.
Common pitfall: The edges of the tree in the provided input are undirected edges. This makes creating a directed and rooted tree challenging.
Let me know if you have any questions.