Skip to content

Practice Print Binary Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#655
LeetCode Problem

Print Binary Tree

Problem Statement

Create a string matrix showing the given binary tree. If its height measured in edges is h, use h + 1 rows and 2^(h + 1) - 1 columns, initially filled with empty strings. Put the root in the middle column of row 0. For a node at row r and column c, place its left child at row r + 1, column c - 2^(h - r - 1), and its right child at row r + 1, column c + 2^(h - r - 1). Store each node's value as a string and return the completed matrix.

Source: Print Binary Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
root = [1,2]
Output
[["","1",""], ["2","",""]]

# Example 2

Input
root = [1,2,3,null,4]
Output
[["","","","1","","",""], ["","2","","","","3",""], ["","","4","","","",""]]

Constraints

The number of nodes in the tree is in the range [1, 2^10].
-99 <= Node.val <= 99
The depth of the tree will be in the range [1, 10].

Related LeetCode Problems

Practice more problems similar to Print Binary Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.