-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.cs
139 lines (111 loc) · 4.34 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Problem: https://www.hackerrank.com/challenges/swap-nodes-algo/problem
C# Language Version: 7.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
1. Build the tree with n nodes. Each node should maintain the level at which it lies. Let the root node numbered 1 is called r.
2. Let the number of swap operations be t.
3. Run the loop to take t inputs:
3.1 Let the next input number be k.
3.2 Perform level order traversal of tree starting from r.
3.3 For each node encountered during level order traversal, if its level is a multiple of k then swap its child nodes.
3.4 Now perform in-order traversal starting from r and print all number of all the nodes.
3.5 Repeat steps from 3.1 to 3.4 for each input number obtained in the loop.
Time Complexity: O(n) //there is only one for loop.
Space Complexity: O(n)
//In-order traversal requires n stack frames as it involves recursion.
//swapping involves level order traversal which again requires O(n) space for queue.
*/
using System;
using System.Collections.Generic;
class Solution
{
static string[] nodeData;
static void Main(String[] args)
{
//numberOfNodes
var n = int.Parse(Console.ReadLine());
var root = new TreeNode { Data = 1, LeftChild = null, RightChild = null, Depth = 1 };
nodeData = new string[n];
for (var i = 0; i < n; i++)
nodeData[i] = Console.ReadLine();
BuildTree(root);
var t = int.Parse(Console.ReadLine());
for (int i = 0; i < t; i++)
{
var k = int.Parse(Console.ReadLine());
//for k perform swap operations at level k, 2k, 3k...
PerformSwapDuringLevelOrderTraversal(root, k);
//after all above swappings
PrintInOrderTraversal(root);
Console.WriteLine();
}
}
private static void PerformSwapDuringLevelOrderTraversal(TreeNode node, int k)
{
//use a queue here for level order traversal for swapping children of all the designated nodes at a given level.
var nextLevelNodes = new Queue<TreeNode>();
if (node.Depth % k == 0)
SwapChildNodes(node); //depth of current node is a multiple of k so swapping is needed
EnqueueChildrenToQueue(nextLevelNodes, node);
while (nextLevelNodes.Count > 0)
{
var nextNode = nextLevelNodes.Dequeue();
if (nextNode.Depth % k == 0)
SwapChildNodes(nextNode);
EnqueueChildrenToQueue(nextLevelNodes, nextNode);
}
}
private static void EnqueueChildrenToQueue(Queue<TreeNode> nextLevelNodes, TreeNode node)
{
if (node.LeftChild != null)
nextLevelNodes.Enqueue(node.LeftChild);
if (node.RightChild != null)
nextLevelNodes.Enqueue(node.RightChild);
}
private static void SwapChildNodes(TreeNode node)
{
var temp = node.LeftChild;
node.LeftChild = node.RightChild;
node.RightChild = temp;
}
private static void PrintInOrderTraversal(TreeNode root)
{
if (root.LeftChild != null)
PrintInOrderTraversal(root.LeftChild);
Console.Write(root.Data + " ");
if (root.RightChild != null)
PrintInOrderTraversal(root.RightChild);
}
private static void BuildTree(TreeNode node)
{
var childData = nodeData[node.Data - 1];
var splitted = childData.Split(' ');
var leftChildData = int.Parse(splitted[0]);
var rightChildData = int.Parse(splitted[1]);
if (leftChildData == -1)
node.LeftChild = null;
else
{
var leftNode = new TreeNode { Data = leftChildData, Depth = node.Depth + 1 };
node.LeftChild = leftNode;
BuildTree(leftNode);
}
if (rightChildData == -1)
node.RightChild = null;
else
{
var rightNode = new TreeNode { Data = rightChildData, Depth = node.Depth + 1 };
node.RightChild = rightNode;
BuildTree(rightNode);
}
}
}
class TreeNode
{
public int Data { get; set; }
public TreeNode LeftChild { get; set; }
public TreeNode RightChild { get; set; }
public int Depth { get; set; }
}