Swap Nodes [Algo] HackerRank Solution


Swap Nodes [Algo] HackerRank Solution
Source : https://www.hackerrank.com/challenges/swap-nodes-algo



Source : https://www.hackerrank.com/challenges/swap-nodes-algo


Solution


// Karthikalapati.blogspot.com
import java.util.Scanner;
// Main trick: Repesent our tree as a 1-D array
public class Solution {
public static void main(String[] args) {
/* Create tree */
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
Pair [] array = new Pair[N+1]; // represents our tree
for (int i = 1; i <= N; i++) {
array[i] = new Pair(scan.nextInt(), scan.nextInt());
}
/* Perform swaps and print inorder traversals */
int T = scan.nextInt();
while (T-- > 0) {
int K = scan.nextInt();
swap(array, K, N);
inorderTraverse(array, 1);
System.out.println();
}
scan.close();
}
private static void swap(Pair [] array, int K, int maxDepth) {
for (int h = K; h <= maxDepth; h += K) {
swap(array, h, 1, 1);
}
}
private static void swap(Pair [] array, int depthToSwap, int currIndex, int currDepth) {
if (currIndex < 1) {
return;
}
Pair p = array[currIndex];
if (currDepth == depthToSwap) {
p.swapSubtrees();
} else {
swap(array, depthToSwap, p.left, currDepth + 1);
swap(array, depthToSwap, p.right, currDepth + 1);
}
}
private static void inorderTraverse(Pair [] array, int index) {
if (index != -1) {
inorderTraverse(array, array[index].left);
System.out.print(index + " ");
inorderTraverse(array, array[index].right);
}
}
}
class Pair {
int left;
int right;
public Pair(int left, int right) {
this.left = left;
this.right = right;
}
public void swapSubtrees() {
int temp = left;
left = right;
right = temp;
}
}

8 comments:

  1. I am jovial you take pride in what you write. It makes you stand way out from many other writers that can not push high-quality content like you. Nft opensea

    ReplyDelete
  2. OBCTOP sebagai agen slot resmi telah bekerja sama dengan puluhan provider-provider khusus permainan slot didunia seperti Pragmaticplay, Habanero, PGSoft hingga Isoftbet. slot online

    ReplyDelete
  3. steam cleaners can clean lots of dirty clothes in a very short period of time that is why i always prefer to use them’ 안전놀이터

    ReplyDelete
  4. The blog and data is excellent and informative as well qureka banner

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. I love to most of the information, I must claim when i liked, When i would love additional information related to that, because it's very good., Appreciate it about unveiling. Intian e viisumikelpoisuus australian kansalaiset

    ReplyDelete
  7. Easily this fabulous website may perhaps it goes without saying be well-known within many blog persons, with the aware articles or reviews or perhaps opinions. Intian e-viisumikelpoisuus antiguan ja barbudan kansalaisille

    ReplyDelete
  8. One time i actually got on your webpage even if finding account simply to some extent great deal submits. Fulfilling technique for coming, We're book-marking together receive styles ending rises up. india visa eligibility india visa for argentinian citizens

    ReplyDelete