FAQ: Binary Search Trees: Python - Traversing A Binary Search Tree

This community-built FAQ covers the “Traversing A Binary Search Tree” exercise from the lesson “Binary Search Trees: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Pass the Technical Interview with Python

FAQs on the exercise Traversing A Binary Search Tree

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Could you please explain why in binary tree traversal the print function is only done for the left child, but at the output we receive all tree?

1 Like

There might be a bug at step 4. This is the method I wrote:

  def depth_first_traversal(self):
    if self.left:
      self.left.depth_first_traversal()
    print(f'Depth={self.depth}, Value={self.value}')
    if self.right:
      self.right.depth_first_traversal()

my solution for the code was not accepted. Also when I manually replaced my code with the offered solution it wouldn’t accept the input. Only by selecting the option to replace my code with the solution the input was accepted and I could continue the lesson.

5 Likes

Same problem over here, wrote exactly the same code, but it is not accepted.

3 Likes

Same Issue here as well. Wrote the same code and both the Get Unstuck version of the code and the above verion of the code produces the same results. If it is not a bug and there is a reason to use:

  def depth_first_traversal(self):
    if (self.left is not None):
      self.left.depth_first_traversal()
    print(f'Depth={self.depth}, Value={self.value}')
    if (self.right is not None):
      self.right.depth_first_traversal()

Over:

  def depth_first_traversal(self):
    if self.left:
      self.left.depth_first_traversal()
    print(f'Depth={self.depth}, Value={self.value}')
    if self.right:
      self.right.depth_first_traversal()

The reason would definitely be a good addition to the lesson.

I have to assume readability but I also assume the readability of a truthy/falsey statement, would not be an issue for any advanced Python users.

Kind Regards to all! :slight_smile:

2 Likes

I was having the same issue and came to the Forums to see if others were having the same problem. I also would like to know if there is a practical reason for it or if it is just a bug

If the issue is exactly as described by @code5492237692, then it is definitely a bug.
@catower, could you look into this, please?

2 Likes
  def depth_first_traversal(self):
    if self.left is not None:
      self.left.depth_first_traversal()
      print(f'Depth={self.depth}, Value={self.value}')
    if self.right is not None:
      self.right.depth_first_traversal()
      print(f'Depth={self.depth}, Value={self.value}')

I got an error even in this situation

It doesn’t accepts if we’re using

  • if self.left / if self.right

  • print in both if statements

It’d only accept if print was after the if self.left condition

It’s been a year and they are yet to fix it!

Same here.
It also only accepts single quotation marks when it comes to the f-string