Swap Nodes(Algo) (Hackerrank)
Question -
A binary tree is a tree which is characterized by one of the following properties:
It can be empty (null).
It contains a root node only.
It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees.
In-order traversal is performed as
Traverse the left subtree.
Visit root.
Traverse the right subtree.
For this in-order traversal, start from the left child of the root node and keep exploring the left subtree until you reach a leaf. When you reach a leaf, back up to its parent, check for a right child and visit it if there is one. If there is not a child, you've explored its left and right subtrees fully. If there is a right child, traverse its left subtree then its right in the same manner. Keep doing this until you have traversed the entire tree. You will only store the values of a node as you visit when one of the following is true:
it is the first node visited, the first time visited
it is a leaf, should only be visited once
all of its subtrees have been explored, should only be visited once while this is true
it is the root of the tree, the first time visited
Swapping: Swapping subtrees of a node means that if initially node has left subtree L and right subtree R, then after swapping, the left subtree will be R and the right subtree, L.
For example, in the following tree, we swap children of node 1.
Depth
1 1 [1]
/ \ / \
2 3 -> 3 2 [2]
\ \ \ \
4 5 5 4 [3]
In-order traversal of left tree is 2 4 1 3 5 and of right tree is 3 5 1 2 4.
Swap operation:
We define depth of a node as follows:
The root node is at depth 1.
If the depth of the parent node is d, then the depth of current node will be d+1.
Given a tree and an integer, k, in one operation, we need to swap the subtrees of all the nodes at each depth h, where h ∈ [k, 2k, 3k,...]. In other words, if h is a multiple of k, swap the left and right subtrees of that level.
You are given a tree of n nodes where nodes are indexed from [1..n] and it is rooted at 1. You have to perform t swap operations on it, and after each swap operation print the in-order traversal of the current state of the tree.
Code-
#include<bits/stdc++.h>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
vector<int> leftnode , rightnode;
int swap_level;
//inorder traversal left->node->right
void traversal(int node=1)
{
if(node==-1)
return;
traversal(leftnode[node]);
cout<<node<<" ";
traversal(rightnode[node]);
if(node==1)
cout<<endl;
}
//swapping function
void swap(int level=1, int node=1)
{
if(node==-1)
return;
if(level%swap_level==0) //on the same level
{//swap left to right at that level
int temp=leftnode[node];
leftnode[node]=rightnode[node];
rightnode[node]=temp;
}
//recursively increase depth of level and swap for the same again
swap(level+1, leftnode[node]);
swap(level+1, rightnode[node]);
}
int main()
{
int count;
cin>>count;
//initialise right and left nodes as 0
leftnode.push_back(0);
rightnode.push_back(0);
while(count--)
{
int l,r;
cin>>l>>r;
//input left and right values resp.
leftnode.push_back(l);
rightnode.push_back(r);
}
cin>>count;
while(count--)
{
cin>>swap_level;
swap(); //swap at level-> swap_level
traversal(); //traverse arrays until all levels are swapped resp.
}
return 0;
}