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.

The swapping part of the question was actually simple. Getting the input and converting into the tree was actually a bit tedious because of the way the input was given.
Actual swapping could be done just with a simple addition to normal inorder traversal once the tree was built(which required queue, to simplify the process of creating it.)
Here's my swap function. Hope it helps in understanding the problem for anyone who actually found swapping ambiguous:

Wow! Thats what I wanted to learn. I did the same thing with all the basic concepts, because I got tired with so much work in this problem. But your elegant code took it to next level

static void swapSPL(int k){
int btLevels=maxDepth(root);
//System.out.print("K="+k+", NumberOfLevels="+btLevels);
int j=1;
for(int i=k;i
{
if (node == null)
return;
if (k == 1)
{
//System.out.print(" Swaping for this "+node.key + " ");
Node temp=node.right;
node.right= node.left;
node.left=temp;
return;
}
else
{
swapKDistant(node.left, k - 1);
swapKDistant(node.right, k - 1);
}
}

Agree. building the tree is more difficult/complex than swapping. Would you please share your c/c++ code of building the tree with queue or other method here? thanks!

## Swap Nodes [Algo]

You are viewing a single comment's thread. Return to all comments →

The swapping part of the question was actually simple. Getting the input and converting into the tree was actually a bit tedious because of the way the input was given. Actual swapping could be done just with a simple addition to normal inorder traversal once the tree was built(which required queue, to simplify the process of creating it.) Here's my swap function. Hope it helps in understanding the problem for anyone who actually found swapping ambiguous:

Wow! Thats what I wanted to learn. I did the same thing with all the basic concepts, because I got tired with so much work in this problem. But your elegant code took it to next level

static void swapSPL(int k){ int btLevels=maxDepth(root); //System.out.print("K="+k+", NumberOfLevels="+btLevels); int j=1; for(int i=k;i { if (node == null) return; if (k == 1)

{ //System.out.print(" Swaping for this "+node.key + " "); Node temp=node.right; node.right= node.left; node.left=temp; return; }

else { swapKDistant(node.left, k - 1); swapKDistant(node.right, k - 1); } }

Agree. building the tree is more difficult/complex than swapping. Would you please share your c/c++ code of building the tree with queue or other method here? thanks!

give you email .ill email it to you

Hi, Kunal,

Thanks. you may contact me on cindygaopan@hotmail.com

thanks,

hey, can you forward me the code too if you have recivied the code over this mail ID: harindermaheshwari@gmail.com