If This Answer Is Correct Then Is This?

Link to quiz(I am giving it to you because I want you to do some testing for me):
https://www.codecademy.com/courses/learn-data-structures-and-algorithms-with-python/quizzes/binary-search-trees-python

Look:

I got it right but it would also be valid if I switched the blocks around like this:


(sorry, the writing is not neat but I had to write it using my mouse which made lose some control).
Do you think it would mark me wrong if I do that way since I think there is supposed to be 1 right answer? If you aren’t sure then take the quiz do it the way that is the above way’s picture’s solution on the corresponding question.

The question asks for you to complete the code to print an in-order traversal of a binary tree. Therefore you always traverse first from the left of the tree as the left will always be the smallest node for that section. Therefore if you swapped them round, you would be traversing to the right first which would not meet the conditions of an in-order traversal. You can see the start of this article for an example.

The gist is that in-order traversal must be in ascending order, and as the left child is always smaller than the right child we must traverse from the left first to be valid in this case.

2 Likes