-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path655.cpp
38 lines (38 loc) · 1.01 KB
/
655.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
// preorder.cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<string>> printTree(TreeNode *root) {
int m = getHeight(root);
if (m == 0)
return vector<vector<string>>(1, vector<string>(1, ""));
int n = (1 << m) - 1;
vector<vector<string>> res(m, vector<string>(n, ""));
fillVector(res, root, 0, n - 1, 0);
return res;
}
void fillVector(vector<vector<string>> &res, TreeNode *root, int l, int r,
int h) {
if (!root)
return;
int mid = (l + r) / 2;
res[h][mid] = to_string(root->val);
fillVector(res, root->left, l, mid - 1, h + 1);
fillVector(res, root->right, mid + 1, r, h + 1);
}
int getHeight(TreeNode *root) {
if (root == nullptr)
return 0;
int l = getHeight(root->left);
int r = getHeight(root->right);
return l > r ? l + 1 : r + 1;
}
};