-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
42 lines (40 loc) · 1.28 KB
/
solution.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
39
40
41
42
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
/**
* 73 / 73 test cases passed.
* Runtime: 4 ms
* Memory Usage: 8 MB
*/
class Solution {
public:
int getDepth(TreeNode* node) {
if (!node) return -1;
return max(getDepth(node->left), getDepth(node->right)) + 1;
}
vector<vector<string>> printTree(TreeNode* root) {
int height = getDepth(root);
int m = height + 1;
int n = (1 << m) - 1;
vector<vector<string>> ans(m, vector<string>(n, ""));
queue<tuple<TreeNode*, int, int>> q;
q.push({root, 0, (n - 1) / 2});
while (!q.empty()) {
auto t = q.front(); q.pop();
TreeNode* node = get<0>(t);
int r = get<1>(t), c = get<2>(t);
ans[r][c] = to_string(node->val);
if (node->left) q.push({node->left, r + 1, c - (1 << (height - r - 1))});
if (node->right) q.push({node->right, r + 1, c + (1 << (height - r - 1))});
}
return ans;
}
};