Given the root of a binary tree, construct a 0-indexed m x n string matrix res that represents a formatted layout of the tree.

The formatted layout matrix should be constructed using the following rules:

  • The height of the tree is height and the number of rows m should be equal to height + 1.
  • The number of columns n should be equal to 2^(height+1) - 1.
  • Place the root node in the middle of the top row (more formally, at location res[0][(n-1)/2]).
  • For each node that has been placed in the matrix at position res[r][c], place its left child at res[r+1][c-2^(height-r-1)] and its right child at res[r+1][c+2^(height-r-1)].
  • Continue this process until all the nodes in the tree have been placed.
  • Any empty cells should contain the empty string "".

Return the constructed matrix res.

Input & Output

Example 1 — Basic Tree
$ Input: root = [1,2,3,null,4]
Output: [["","","","1","","",""],["","2","","","","3",""],["","","4","","","",""]]
💡 Note: Tree height is 2, so matrix is 3×7. Root 1 goes at center (0,3). Node 2 at (1,1), node 3 at (1,5), node 4 at (2,2).
Example 2 — Single Node
$ Input: root = [1]
Output: [["1"]]
💡 Note: Single node tree has height 0, creating 1×1 matrix with just the root node.
Example 3 — Larger Tree
$ Input: root = [1,2,3,4,5,6,7]
Output: [["","","","1","","",""],["","2","","","","3",""],["","4","","5","","6","7"]]
💡 Note: Complete binary tree with height 2. Each level uses power-of-2 spacing: level 0 spacing=4, level 1 spacing=2, level 2 spacing=1.

Constraints

  • The number of nodes in the tree is in the range [1, 210]
  • 0 ≤ Node.val ≤ 109

Visualization

Tap to expand
Print Binary Tree - DFS Approach INPUT Binary Tree Structure: 1 2 3 4 Input Array: [1, 2, 3, null, 4] Height = 2 (0-indexed) Matrix: 3 rows x 7 cols ALGORITHM STEPS 1 Calculate Height DFS to find max depth height = 2 2 Create Matrix rows = height + 1 = 3 cols = 2^(h+1) - 1 = 7 3 Fill with DFS col = (left + right) / 2 Recurse left/right 4 Position Formula Left: col - 2^(h-r-1) Right: col + 2^(h-r-1) Column Positions: Row 0: 1 at col 3 Row 1: 2 at col 1, 3 at col 5 Row 2: 4 at col 2 (mid of [0,6], [0,2], etc) FINAL RESULT 3 x 7 Matrix Output: 1 2 3 4 0 1 2 3 4 5 6 Output Array: [ ["","","","1","","",""], ["","2","","","","3",""], ["","","4","","","",""] OK - Done! Tree printed with proper spacing and alignment Key Insight: The matrix width is 2^(height+1) - 1 to ensure perfect binary tree spacing. Each node's column position is the midpoint of its subtree range. Children offset by 2^(height-row-1) from parent, creating the characteristic tree shape where spacing halves at each level. DFS fills positions recursively. TutorialsPoint - Print Binary Tree | DFS Approach
Asked in
Facebook 25 Amazon 18 Microsoft 15
34.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen