1
class Solution {
2
public:
3
vector<vector<string>> res;
4

5
int height(TreeNode *root) {
6
if (!root) return 0;
7
return 1 + max(height(root->left), height(root->right));
8
}
9

10
void fill(TreeNode *root, int r, int c, int h) {
11
if (!root) return;
12
res[r][c] = to_string(root->val);
13
fill(root->left, r + 1, c - pow(2, h - r - 1), h);
14
fill(root->right, r + 1, c + pow(2, h - r - 1), h);
15
}
16

17
vector<vector<string>> printTree(TreeNode *root) {
18
int h = height(root);
19
int c = pow(2, h) - 1;
20
res.resize(h, vector<string>(c, ""));
21
fill(root, 0, (c - 1) / 2, h - 1);
22
return res;
23
}
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0