logo CodeStepByStep logo

swapChildrenAtLevel

Language/Type: C++ binary trees pointers recursion
Author: Marty Stepp (on 2016/06/16)

Write a member function named swapChildrenAtLevel that could be added to the BinaryTree class. Your function should accept an integer k as a parameter and swap the left and right children of all nodes at level k. In other words, after your function is run, any node at level (k+1) that used to be its parent's left child should now be its parent's right child and vice versa. For this problem, the overall root of a tree is defined to be at level 1, its children are at level 2, etc. The table below shows the result of calling this member function on a BinaryTree variable named tree.

level tree after tree.swapChildrenAtLevel(2);
1

2

3

4
             42
          /      \
      19           65
     /  \         /
   54    32     23
        /
      12
             42
          /      \
      19           65
     /  \            \
   32    54           23
  /
12

If k is 0 or less than 0, your function should throw an integer exception. If the tree is empty or does not have any nodes at the given level or deeper, it should not be affected by a call to your function.

Constraints: For efficiency, your function should not traverse any parts of the tree that it does not need to traverse. Specifically, you should not access any nodes lower than level (k+1), because there is nothing there that would be changed. Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). You may define private helper functions to solve this problem, but otherwise you may not call any other members of the tree class nor create any data structures such as arrays, vectors, etc. You should not construct any new tree node objects or change the data of any nodes, though you can declare pointers if you like. Your solution must be recursive.

Write the member function as it would appear in BinaryTree.cpp. You do not need to declare the function header that would appear in BinaryTree.h. Assume that you are adding this method to the BinaryTree class as defined below:

class BinaryTree {
private:
    BinaryTreeNode* root;   // nullptr for an empty tree
    ...
    
public: 
    your code goes here;
};

struct BinaryTreeNode {
    int data;
    BinaryTreeNode* left;
    BinaryTreeNode* right;
    ...
}
Type your C++ solution code here:


This is a member function problem. Submit a member function that will become part of an existing C++ class. You do not need to write the complete class, just the member function described in the problem.

You must log in before you can solve this problem.


Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.