Implement a function that prints (or returns) an ASCII layout of a binary tree.
Requirements:
-
Each node has a string value (assume it contains no spaces).
-
If a node is missing a left or right child, print a placeholder
*
in that child position.
-
The bottom row represents the leaf level of a
full
binary tree of height
h
(where
h
is the height of the input tree). It therefore has
2^(h-1)
leaf slots.
-
Leaf slots must be separated by exactly one space.
-
For every level above the leaves, each parent node must be positioned exactly in the middle (horizontally) between its left-child slot and right-child slot.
Return the output as a list of strings (one per level from top/root to bottom/leaves), or print it line by line.
Example (one possible formatting):
If the tree is:
-
root =
A
-
A.left =
B
, A.right =
C
-
B.right =
D
(B.left is null)
Then the bottom level has 4 slots: * D * * (with single spaces), and parents are centered above their two children slots.
Explain any assumptions you make about node value width (e.g., single character vs variable-length strings).