Class Solution
-
- All Implemented Interfaces:
public final class Solution655 - Print Binary Tree\.
Medium
Given the
rootof a binary tree, construct a 0-indexedm x nstring matrixresthat represents a formatted layout of the tree. The formatted layout matrix should be constructed using the following rules:The height of the tree is
heightand the number of rowsmshould be equal toheight + 1.The number of columns
nshould be equal to <code>2<sup>height+1</sup> - 1</code>.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 <code>resc-2<sup>height-r-1</sup></code> and its right child at <code>resc+2<sup>height-r-1</sup></code>.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.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 <code>1, 2<sup>10</sup></code>.
-99 <= Node.val <= 99The depth of the tree will be in the range
[1, 10].
-
-
Constructor Summary
Constructors Constructor Description Solution()
-