Problem
Print a binary tree in an m*n 2D string array following these rules:
- The row number
m
should be equal to the height of the given binary tree. - The column number
n
should always be an odd number. - The root node’s value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don’t need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don’t need to leave space for both of them.
- Each unused space should contain an empty string
""
. - Print the subtrees following the same rules.
Example 1:
Input: 1 / 2 Output: [["", "1", ""], ["2", "", ""]]
Example 2:
Input: 1 / \ 2 3 \ 4 Output: [["", "", "", "1", "", "", ""], ["", "2", "", "", "", "3", ""], ["", "", "4", "", "", "", ""]]
Example 3:
Input: 1 / \ 2 5 / 3 / 4 Output: [["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""] ["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""] ["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""] ["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]]
Note: The height of binary tree is in the range of [1, 10].
Solution: Recursion
Compute the layers h of the tree.
shape of the output matrix: h * w (w = 2^h – 1)
pre-order to fill the output matrix
first layer’s root: y1 = 0, x1 = (l1 + r1) / 2 = (0 + w – 1) / 2 (center)
first layer’s left child (2nd layer): y2 = 1, x2 = (l2 + r2) = (l1 + (x1 – 1)) / 2 (center of the left half)
first layer’s right child(2nd layer): y1 = 2, x2 = (l2′ + r2′) = ((x1+1) + r1) / 2 (center of the right half)
…
Time complexity: O(m*n)
Space complexity: O(m*n)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Author: Huahua // Running time: 4 ms class Solution { public: vector<vector<string>> printTree(TreeNode* root) { int h = getHeight(root); int w = (1 << h) - 1; // 2 ^ h - 1 vector<vector<string>> ans(h, vector<string>(w, "")); fill(root, ans, 0, 0, w - 1); return ans; } private: int getHeight(TreeNode* root) { if (!root) return 0; return max(getHeight(root->left), getHeight(root->right)) + 1; } void fill(TreeNode* root, vector<vector<string>>& ans, int h, int l, int r){ if (!root) return; int mid = (l + r) / 2; ans[h][mid] = std::to_string(root->val); fill(root->left, ans, h + 1, l, mid - 1); fill(root->right, ans, h + 1, mid + 1, r); } }; |
Python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
""" Author: Huahua Running time: 44 ms """ class Solution: def printTree(self, root): def height(root): if not root: return 0 return max(height(root.left), height(root.right)) + 1 def fill(root, h, l, r): if not root: return mid = (l + r) // 2 m[h][mid] = str(root.val) if root.left: fill(root.left, h + 1, l, mid - 1) if root.right: fill(root.right, h + 1, mid + 1, r) h = height(root) w = 2**h - 1 m = [[""] * w for _ in range(h)] fill(root, 0, 0, w - 1) return m |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment