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.
I agree with you specially with the fact that it uses a lot of concepts from the other problems of trees section. I was able to achieve it using a simple algorithm:
Build the tree.
Repeat below steps for every value of k
Perform level order traversal (BFS) of the entire tree.
At each level, if level is a multiple of k then swap the
child of the nodes at that level.
After the level order traversal is done, perform in order traversal (DFS) to print the tree nodes data.
I used below definition of a node while building the tree:
class TreeNode
{
public int Data { get; set; }
public TreeNode LeftChild {get; set;}
public TreeNode RightChild { get; set; }
public int Depth { get; set; }
}
Swap Nodes [Algo]
You are viewing a single comment's thread. Return to all comments →
I agree with you specially with the fact that it uses a lot of concepts from the other problems of trees section. I was able to achieve it using a simple algorithm:
Repeat below steps for every value of k
child of the nodes at that level.
I used below definition of a node while building the tree: