can you publish the display tree function?
I made my own but it doesn’t quite work like the one you guys have? How can I fix it?
def display_tree(root):
# Function to print the binary tree with dashes between nodes
if not root:
print(“Empty Tree”)
return
# Queue to hold the nodes
queue = deque()
queue.append(root)
while queue:
# Current level nodes count
level_size = len(queue)
for _ in range(level_size):
node = queue.popleft()
if node:
# Print the node's data
print(node.data, end='')
# Add left and right children to the queue
queue.append(node.left)
queue.append(node.right)
# Print a dash after the node if it has children
if node.left or node.right:
print('-', end=' ')
else:
# Print 'X' if node is None
print('X', end=' ')
# Move to the next level
print()
Course: Grokking Coding Interview Patterns in Python - Learn Interactively
Lesson: Solution: Lowest Common Ancestor in a Binary Tree - Grokking Coding Interview Patterns in Python