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
|
195 Discussions
|
Please Login in order to post a comment
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 {
}
java8 working f9- import java.util.*;
enum Color { RED, GREEN }
abstract class Tree { private int value; private Color color; private int depth;
}
class TreeNode extends Tree { private 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); }
class SumInLeavesVisitor extends TreeVis { private int sum = 0;
}
class ProductRedNodesVisitor extends TreeVis { private long product = 1; private final int MOD = 1000000007;
}
class FancyVisitor extends TreeVis { private int evenDepthNonLeafSum = 0; private int greenLeafSum = 0;
}
public class Solution { public static Tree solve() { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] values = new int[n]; byte[] colors = new byte[n];
}
This problem leads one to believe that the input data is a tree. But in fact, it is an undirected graph. The lack of this information made me waste a lot of time solving the problem.