• + 5 comments

    It's not ambiguous at all. It's not a very deep problem. If you code it clean you won't have any difficulties. Btw, it uses a lot of concepts from the other problems of the Trees section: https://www.hackerrank.com/domains/data-structures/trees.

    • + 3 comments

      It's not about difficulty. it's about clarity.

      • + 1 comment

        me too facing too dificulty in understanding the problem

        • + 2 comments

          i am also facing dificulty in understanding

          • + 0 comments

            me too, one of the less clear problems

          • + 1 comment

            Almost had a seizure while going through the description..

            • + 1 comment

              hahaha, agreed. I came to the discussion session to try and understand it

      • + 0 comments

        Agree. It is unnecessarily wordy.

      • + 3 comments

        In the first part it says that the input is an array, and then it shows something that looks like a multi-dimensional array as an input sample. So which is it? An array or a multi-dimensional array?

        • + 0 comments

          If you test print the indexes 2 D array, you can see that it will print exactly what has been given as input. However, my doubt is whether 1 is the root node in every case because the description hasn't accepted 1 in input but in the explanation they have kept root node=1.

        • + 0 comments

          A multidimensional array is an array, its an array of arrays

        • + 0 comments

          I agree, if you say that you are giving me a 2D array then give me a 2D array, not a list of lists! (solving it as Java 8)

    • + 2 comments

      Yes, it is ambiguous. In one case it says to print the traversal after each swap operation (which for each T there can be more than 1), then it implies that the traversal only needs to be printed T times.

      It would not have taken much effort to make this clear.

      • + 2 comments

        Actually, a swap operation consists of more swaps, see the definition for Swapping and for Swap operation, so the problem is not ambiguous when it comes to that part.

        • + 2 comments

          Input is somehow ambiguous, for example

          3

          2 3

          -1 -1

          -1 -1

          2

          1

          1

          How can you assume that the root is 1 and another child is 2? And why are you swapping 1 twice? And what's the meaning of the -1 -1, -1 -1. Makes no sense to me.

          • + 1 comment

            I think all the information you need is in the problem description. It's just a lot to take in.

            The tree with N nodes is rooted at the node with index 1 as described in the Swapping operation section. So the root of the tree is 1.

            -1 is used to represent a null node, as described in the Input Format section. For example, the -1s in the second of the N lines mean that the node with index 2 has NULL pointers for both of its children. Similarly, the -1s in the third of the N lines means that the node with index 3 also has NULL pointers for both of its children.

            I don't think there's a particular reason why we swap at level 1 twice. The sample inputs are just used to illustrate an example (Sample Input/Output #00 correspond to Test Case #00 in the Explanation section).

            • + 2 comments

              Index 1 and the value of the root are not the same. Binary tree root can contain any value. That part of the problem declaration is ambiguous. For example:

                     5
                   /   \
                  3     2
                 / \   / \
                10  6 9  17
              

              Is still a binary tree. It's not sorted, but that does not disqualify it as a binary tree.

              • + 1 comment

                The problem says "You are given a tree of n nodes where nodes are indexed from 1..n and it is rooted at 1. " So we can safely assume that the binary tree we will be given for this problem always has a root of 1. I dealt with this by creating a BinaryTree object with a root of 1.

                • + 1 comment

                  What does rooted at 1 mean ? It is ambiguous. Rooted at index 1 or the root element is 1 ?

                  • + 0 comments

                    yes. It means the tree has a root node with value '1'.

              • + 0 comments

                Binary search tree is sorted not the Binray tree

        • + 0 comments

          Yes. Using an OOP ways can make the solution more simpler swap operation. But, yet again, the problem is not just about swaping but doing the node swap with such inconvienient input.

      • + 1 comment

        here is problem solution in javascript c++ c python and java programming.

        hackerrank swap nodes [algo] solution

        • + 0 comments

          How is this the answer? The whole code was rewrote. The problem says to finish the swapNodes function. This answer rewrites the solution class as well.

    • + 0 comments

      I got to learn a lot!

    • + 0 comments

      Yes it's as "not ambiguous" as the number of downvotes on you comment :P

    • + 6 comments

      I agree with you specially with the fact that it uses a lot of concepts from the other problems of trees section. I was able to achieve it using a simple algorithm:

      1. Build the tree.
      2. Repeat below steps for every value of k

        • Perform level order traversal (BFS) of the entire tree.
        • At each level, if level is a multiple of k then swap the
          child of the nodes at that level.
        • After the level order traversal is done, perform in order traversal (DFS) to print the tree nodes data.

      I used below definition of a node while building the tree:

      class TreeNode
      {
          public int Data { get; set; }
          public TreeNode LeftChild {get; set;}
          public TreeNode RightChild { get; set; }
          public int Depth { get; set; }
      }
      
      • + 1 comment

        My algorithm is the same as yours,but i did not pass all the test.

        • + 1 comment

          Yeah, same here. I only passed some tests. I wonder what we're doing wrong...

          • + 1 comment

            you're probably missing the bit where if k=2, then you have to swap children at depth =2, depth =4, depth=6 and so on

            • + 0 comments

              It was after i passed 2 out of 3 test that i re-read the problem definition and understood this.

      • + 0 comments

        Thank you for this reply,

        I spent like half an hour debugging with custom inputs only to find out I missed the part where you needed to swap at every depth multiple of k.

        After that, my first algorithm was indeed correct :)

      • + 0 comments

        my algo is similar to yours but i am not able to clear 3 tests cases!

      • + 0 comments

        You don't need to build the tree at all. And you can swap and build the answer in one pass. Well, it might depend on the language and datastructure you're given.

      • + 1 comment

        can you tell the algo for tree creation. I'm getting an issue in that

        • + 1 comment
          Here  it is in java:
                      int numberOfNodes = indexes.length;
              int numOfQueries= queries.length;
          
              int[][] result =new int[numOfQueries][numberOfNodes];
          
              Node root = new Node(1,1);
              Node curr = root;
          
              Queue<Node> nodes = new LinkedList<>();
              nodes.offer(curr);
          
              //tree creation
              int N = 0;
              while (N<numberOfNodes)
              {
                  curr =nodes.poll();
          
                  int leftNode = indexes[N][0];
                  int rightNode = indexes[N][1];
          
                  curr.left = leftNode == -1?null:new Node(leftNode,curr.depth+1);
                  curr.right = rightNode == -1?null:new Node(rightNode,curr.depth+1);
          
                              //Now we are going to add the nodes for next iteration to create node
          
                  if (curr.left!=null && curr.left.data != -1)
                      nodes.offer(curr.left);
                  if(curr.right!=null && curr.right.data != -1)
                      nodes.offer(curr.right);
                  N++;
                              }//loop over
          
              }
          
          • + 0 comments

            Excellent idea of using Queue to track Nodes. Helped me solve this problems correctly.

      • + 0 comments

        Can you tell me how do you know which depth you are in? When you are making the tree you can set the data as the index and the left and right as well but how do you set the depth?