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.
- Prepare
- Java
- Advanced
- Java Visitor Pattern
- Discussions
Java Visitor Pattern
Java Visitor Pattern
Sort by
recency
|
174 Discussions
|
Please Login in order to post a comment
If you pass test case 0 but fail other test cases, you might be encountering the overflow issue when multiplying two integers; the correct way is store result in a long variable
Implement in Java15.
import java.util.ArrayList; import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
import java.util.ArrayList; import java.util.Scanner;
enum Color { RED, GREEN }
abstract class Tree {
}
class TreeNode extends Tree {
}
class TreeLeaf extends Tree {
}
abstract class TreeVis { public abstract int getResult();
}
class SumInLeavesVisitor extends TreeVis { int result = 0;
}
class ProductOfRedNodesVisitor extends TreeVis { long result = 1L;
}
class FancyVisitor extends TreeVis { int sumOfNode = 0; int sumOfLeaf = 0;
}
public class Solution {
}
what is wrong with below code? every node is added at right location but last 2 (leaf) nodes of 0th test case are not visible
public class Solution { public static Boolean check(int nodev,List u){ for(int x:u){ if (nodev==x) return true; } return false; } public static int index(int value,List t){ for(int i=0;i ele=new ArrayList<>(); List col=new ArrayList<>(); List u=new ArrayList<>(); List v=new ArrayList<>();
After putting far too much time into reworking code that passed the 0-th test case and no others, I finally ended up with a proper solution which immediately passed every test case. I have to say, it feels more than a little malicious that the only visible test case lists every edge in (parent, child) order, yet apparently none of the hidden test cases do so. And for a challenge titled "Java Visitor Pattern", there's really no reason for any significant portion - much less the brunt - of the challenge to be in understanding the structure of a specific representation of an adjacency list rather than in understanding, say, the Visitor Pattern.
Solution:
Let's break down the solution step by step:
Visitor Classes
The code defines three visitor classes that extend the abstract class
TreeVis
. Each visitor implements specific logic for traversing and processing nodes and leaves of the tree.SumInLeavesVisitor
:TreeVis
.result
to store the sum.getResult()
to return the calculated sum.visitNode(TreeNode node)
with an empty implementation because this visitor does not operate on internal nodes.visitLeaf(TreeLeaf leaf)
to add the value of the leaf node to the sum.ProductOfRedNodesVisitor
:TreeVis
.result
to store the product.getResult()
to return the calculated product.visitNode(TreeNode node)
to check if the node is red and update the product accordingly.visitLeaf(TreeLeaf leaf)
to check if the leaf is red and update the product accordingly.FancyVisitor
:TreeVis
.sumOfNode
andsumOfLeaf
to store the sums.getResult()
to return the absolute difference betweensumOfNode
andsumOfLeaf
.visitNode(TreeNode node)
to check if the node depth is even and updatesumOfNode
.visitLeaf(TreeLeaf leaf)
to check if the leaf is green and updatesumOfLeaf
.solve
MethodThe
solve
method is responsible for constructing the tree based on input data obtained from standard input (STDIN). Here's a breakdown of this method:Read Input:
Scanner
object to read input.n
representing the number of nodes in the tree.vals
andcolors
to store node values and colors, respectively.Populate
vals
andcolors
Arrays:n
integers into thevals
array, representing node values.n
integers (0 or 1) into thecolors
array and converts them toColor.RED
orColor.GREEN
.Build Node Edges:
nodeEdges
to store the edges between nodes.n - 1
pairs of integersu
andv
representing edges between nodesu
andv
.nodeEdges
accordingly.Construct Tree:
nodeIndexMap
to store node indices.parents
to track parent nodes.TreeNode
) using the first value and color fromvals
andcolors
.parents
and sets its index innodeIndexMap
.parents
is empty:nextLevelParents
list for the next level of nodes.parents
:nodeIndexMap
.nodeEdges
corresponding to the parent index:nodeEdges
.TreeNode
orTreeLeaf
based on the child index, value, color, and depth.nodeIndexMap
.nextLevelParents
.TreeLeaf
and adds it to the parent node.parents
with thenextLevelParents
list.Return Root:
Scanner
.In summary, the
solve
method parses input data to build a tree structure with nodes and edges, ensuring correct parent-child relationships. It constructs internal nodes (TreeNode
) and leaf nodes (TreeLeaf
) based on the input indices, values, colors, and edges. Finally, it returns the root of the constructed tree.