We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Data Structures
- Trees
- Swap Nodes [Algo]
- Discussions
Swap Nodes [Algo]
Swap Nodes [Algo]
Sort by
recency
|
720 Discussions
|
Please Login in order to post a comment
This Problem Is A Prime Example Of Why Hackerrank Is Such A Bad Platform
In HR, the input to the function isn't always similar to the input to the program/submission. Alright. But in this case the function they want us to implement takes the input as a 2D array and outputs the same data structure as a 1D array. This adds an unnecessary layer of complexity on top of the actual problem in the question.
The platform should stay consistant with the input and output representation. Why the hell would a tree look like A in the input but look like B in the output? It's a poor design choice that just makes file io easier for the person who made the question. Not to mention that in an interview we have to understand everything and nail both the problem and structuring the output in 30 minutes to get a job.
My Java 8 solution, which passes all test cases, for Max Score of 40:
I iterate over the entire tree structure as outlined in
List<List<Integer>> indexes
, and then I make aMap<Integer, Integer> nodeDepthMap
, which maps a node to its corresponding depth in the tree. I also make aMap<Integer, List<Integer>> depthNodesLstMap
which maps a givendepth
as Key to a List of Nodes occurring at that Depth as Value. These then come in handy for the next step, when I iterate through all the Queries, and execute them sequentially, performing swaps, and doing Recursive Inorder Traversals, accumulating the intermediate results.(Ignore the verbose debug print statements, as those helped in validating my logic with the initial limited test cases, and I commented it out once I verified that the logic is sound).
Java Solution, using level order traversal using 2 queues Start with putting 1 in the primary queue, 1. poll from primary queue, check if you need to swap the child i.e level % k == 0. 2. add the child indexes in secondary queuue. repeat step 1 and 2 until primary queue is empty. once empty means level is done, go to next level. poll all the indexes from secondary and add into primary. do the inOrderTraversal of the tree and add the list to a list
public static List> swapNodes(List> indexes, List queries) { // Write your code here Queue primary = new LinkedList<>(); Queue secondary = new LinkedList<>();
include
include
include
include
include
using namespace std; vector leftNode, rightNode; int swapLevel;
void traverse(int node=1){ if (node == -1) return; traverse(leftNode[node]); cout << node << " "; traverse(rightNode[node]); if (node == 1) cout << endl; }
void swap(int level=1, int node=1) { if (node == -1) return; if (level % swapLevel == 0) { int tmp = leftNode[node]; leftNode[node] = rightNode[node]; rightNode[node] = tmp; } swap(level+1, leftNode[node]); swap(level+1, rightNode[node]); }
int main() { int count;
cin>>count; leftNode.push_back(0); rightNode.push_back(0); while(count--){ int L, R; cin>>L>>R; leftNode.push_back(L); rightNode.push_back(R); } cin>>count; while(count--){ cin >> swapLevel; swap(); traverse(); } return 0; }