leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0655.cpp (797B)


0 class Solution {
1 int height(const TreeNode *root) {
2 if (!root) return 0;
3 return 1 + max(height(root->left), height(root->right));
4 }
6 typedef tuple<TreeNode *, int, int> record;
8 public:
9 vector<vector<string>> printTree(TreeNode *root) {
10 const int n = height(root), m = (1 << n) - 1;
11 vector<vector<string>> res(n, vector<string>(m));
13 queue<record> q;
14 q.push({root, 0, (m - 1) / 2});
15 while (!q.empty()) {
16 const auto [root, r, c] = q.front();
17 q.pop();
18 res[r][c] = to_string(root->val);
19 if (root->left) q.push({root->left, r + 1, c - (1 << (n - r - 2))});
20 if (root->right) q.push({root->right, r + 1, c + (1 << (n - r - 2))});
21 }
22 return res;
23 }
24 };