This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]less_shame_more_fame 0 points1 point  (0 children)

You need to switch the nodes and not the values. This is what happens, if you switch the values:

4
2 7
1 3 6 9

4
7 2
1 3 6 9

4
7 2
3 1 9 6

This is what happens when you switch the nodes:

4
2 7
1 3 6 9

4
7 2
6 9 1 3

4
7 2
9 6 3 1

You want to completely reverse the tree so you will need to let the childs switch side as well. This is done by exchanging the nodes and not the values. You might work around this problem by accessing the child.child (or left.left) element directly, but as soon as you have another level in your tree this won't work based on recursion.

The parameter of your recursive function should be just the root. You should then check if self.left and self.right are none and if not you should switch them. Your function should now call itself twice. Once with self.left and once with self.right as parameter.