Trees and Graphs Comments


  • If it is best practice to make the node value private why isn't it best practice to make the children nodes private and require methods to access them?

  • It may be "better practice" to do that for the Nodes as well, but its simpler to do these algorithms without creating getters and setters for the Node's children.

  • I try to use this code

    but I get back " error: non-static variable this cannot be referenced from a static context Node node = new Node(ar[i]);"

    I am going crazy. Where is the problem?

  • @Salvatore, looking briefly at your code, you should move your Node class out of the Main class (though you'll still have some more work to do).

    public class Main{
      //all of class Main
    class Node{
      //all of class Node
  • Ops, thanks! Meanwhile I figured it out by myself.

    And I am still fighting to find the solution. Keep (all of) you posted!

  • static class Node{
    Node left;
    Node right;
    int key;

        Node(int key){
            this.key = key;
    static void doStuff(int[] ar){
        Node top = ar2SortedTree(ar);
  • Why is "0 2 3 5 4 1" not a correct answer for input #1?

  • It's a valid BFS, but for this challenge you should visit nodes on one level in the order they were in their line of input.

  • That's not specified in the problem statement.

  • Thanks. It was mentioned previously, and I updated it so it's mentioned here as well.

Contact Us
Sign in or email us at [email protected]