fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. import java.util.*;
  9.  
  10. class Ideone {
  11. //public class BetterProgrammerTask {
  12.  
  13. // Please do not change this interface
  14. public static interface Node {
  15. int getValue();
  16. List<Node> getChildren();
  17. }
  18.  
  19. public class Implementation<E> implements Node<E> {
  20.  
  21. private E value;
  22. private List<Node<E>> children;
  23.  
  24. public Implementation(E value, List<Node<E>> children) {
  25. this.value = value;
  26. this.children = children;
  27. }
  28.  
  29. public E getValue() {
  30. return value;
  31. }
  32.  
  33. public List<Node<E>> getChildren() {
  34. return children;
  35. }
  36. }
  37.  
  38.  
  39. public static int getLevelSum(Node root, int N) {
  40. /*
  41.   Please implement this method to
  42.   traverse the tree and return the sum of the values (Node.getValue()) of all nodes
  43.   at the level N in the tree.
  44.   Node root is assumed to be at the level 1. All its children are level 2, etc.
  45.   */
  46.  
  47. // We're at the level we want to sum, return the value
  48. if (N == 1) {
  49. return root.getValue();
  50. }
  51.  
  52. int sum = 0;
  53. for (Node child : root.getChildren()) {
  54. sum += getLevelSum (child, N - 1);
  55. }
  56. return sum;
  57. }
  58.  
  59. public static void main(String[] args){
  60.  
  61. getLevelSum(root,2);
  62. }
  63. }
  64.  
Compilation error #stdin compilation error #stdout 0.1s 27688KB
stdin
Standard input is empty
compilation info
Main.java:19: error: type Node does not take parameters
    public class Implementation<E> implements Node<E> {
                                                  ^
Main.java:22: error: type Node does not take parameters
    private List<Node<E>> children;
                     ^
Main.java:24: error: type Node does not take parameters
    public Implementation(E value, List<Node<E>> children) {
                                            ^
Main.java:33: error: type Node does not take parameters
    public List<Node<E>> getChildren() {
                    ^
Main.java:61: error: cannot find symbol
    	getLevelSum(root,2);
    	            ^
  symbol:   variable root
  location: class Ideone
5 errors
stdout
Standard output is empty