-
Notifications
You must be signed in to change notification settings - Fork 0
/
655. Print Binary Tree.cpp
54 lines (43 loc) · 1.34 KB
/
655. Print Binary Tree.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
43
44
45
46
47
48
49
50
51
52
53
54
// https://leetcode.com/problems/print-binary-tree/
/**
* 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) {}
* };
*/
class Solution
{
public:
vector<vector<string>> printTree(TreeNode *root)
{
vector<vector<string>> result;
int depth = getDepth(root);
int rows = depth;
int columns = pow(2, depth) - 1;
result = vector<vector<string>>(depth, vector<string>(columns));
DFS(root, result, 0, 0, columns - 1);
return result;
}
void DFS(TreeNode *root, vector<vector<string>> &result, int i, int start, int end)
{
if (root == nullptr)
return;
int middle = start + (end - start) / 2;
result[i][middle] = to_string(root->val);
DFS(root->left, result, i + 1, start, middle - 1);
DFS(root->right, result, i + 1, middle + 1, end);
}
int getDepth(TreeNode *root)
{
if (root == nullptr)
return 0;
int left = getDepth(root->left);
int right = getDepth(root->right);
return 1 + max(left, right);
}
};