public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner in = new Scanner(System.in); int N = in.nextInt(); Node[] nodes = new Node[N]; for (int i = 0; i < N; i++) { nodes[i] = new Node(i + 1); } for (int i = 0; i < N; i++) { int a = in.nextInt(); int b = in.nextInt(); Node n = nodes[i]; if (a != -1) n.left = nodes[a - 1]; if (b != -1) n.right = nodes[b - 1]; } int T = in.nextInt(); for (int i = 0; i < T; i++) { int K = in.nextInt(); swap(nodes[0], K, 1); Inorder(nodes[0]); System.out.println(""); } }
static void swap(Node root, int K, int C) { if (root == null) return; if ((C % K) == 0) { Node tmp = root.right; root.right = root.left; root.left = tmp; } C = C + 1; swap(root.left, K, C); swap(root.right, K, C); }