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
|
198 Discussions
|
Please Login in order to post a comment
class SumInLeavesVisitor extends TreeVis { private int sum = 0;
}
class ProductOfRedNodesVisitor extends TreeVis { private double product = 1;
}
class FancyVisitor extends TreeVis { private int nonLeafNodeSum = 0; private int greenLeafSum = 0;
}
public class Solution {
what?!?
java solution with the supposed pre-existing classes
I don't get it are this clases provided? or do we have to implement them? abstract class Tree class TreeNode extends Tree class TreeLeaf extends Tree abstract class TreeVis
because the compiler is not able to find the but the text says: "A Tree class implementing a rooted tree is provided in the editor"
import java.io.; import java.util.;
enum Color { RED, GREEN }
abstract class Tree { private int value; private Color color; private int depth;
}
class TreeNode extends Tree { private final List children = new ArrayList<>();
}
class TreeLeaf extends Tree { public TreeLeaf(int value, Color color, int depth) { super(value, color, depth); }
}
abstract class TreeVis { public abstract int getResult(); public abstract void visitNode(TreeNode node); public abstract void visitLeaf(TreeLeaf leaf); }
// Visitor 1: Sum of all leaf values class SumInLeavesVisitor extends TreeVis { private int sum = 0;
}
// Visitor 2: Product of red node values class ProductOfRedNodesVisitor extends TreeVis { private long product = 1; private final int MOD = 1_000_000_007;
}
// Visitor 3: Difference between even depth node sum and green leaf sum class FancyVisitor extends TreeVis { private int sumEvenDepthNonLeaf = 0; private int sumGreenLeaf = 0;
}
public class Solution {
}