-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path655.java
More file actions
87 lines (87 loc) · 2.89 KB
/
655.java
File metadata and controls
87 lines (87 loc) · 2.89 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
__________________________________________________________________________________________________
sample 1 ms submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<String>> printTree(TreeNode root) {
List<List<String>> res = new ArrayList<>();
if(root==null) return res;
int height = depth(root);
int row = height, col = (int) (Math.pow(2, height)-1);
List<String> cur = new ArrayList<>();
for(int j=0; j<col; j++) cur.add("");
for(int i=0; i<row; i++) res.add(new ArrayList<>(cur));
make(root, res, row, 0, 0, col-1);
return res;
}
public int depth(TreeNode root) {
if(root==null) return 0;
return Math.max(depth(root.left), depth(root.right)) + 1;
}
public void make(TreeNode root, List<List<String>> res, int row, int curRow, int i, int j) {
if(curRow==row || root==null) return;
int mid = i+(j-i)/2;
res.get(curRow).set(mid, Integer.toString(root.val));
make(root.left, res, row, curRow+1, i, mid - 1);
make(root.right, res, row, curRow+1, mid + 1, j);
}
}
__________________________________________________________________________________________________
sample 37264 kb submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<String>> printTree(TreeNode root) {
int h = getHeight(root);
int w = (int)Math.pow(2, h) -1;
System.out.println(w);
List<List<String>> res = new ArrayList<List<String>>();
String[][] ans = new String[h][w];
for(int i =0 ; i< h; i++)
{
for(int j =0;j<w;j++)
{
ans[i][j] = "";
}
}
print(root, 0, 0, w, ans);
for(int i =0 ; i< h; i++)
{
List<String> temp = new ArrayList<>();
for(int j =0;j<w;j++)
{
temp.add(ans[i][j]);
}
res.add(temp);
}
return res;
}
public void print(TreeNode root, int level, int minWidth, int maxWidth, String[][] ans)
{
if (root == null )
return;
ans[level][(minWidth+maxWidth)/2] = ""+root.val;
print(root.left, level+1, minWidth, (minWidth+maxWidth)/2 , ans);
print(root.right, level+1, ((minWidth+maxWidth)/2) +1, maxWidth, ans);
}
public int getHeight(TreeNode root) {
if (root == null)
return 0;
return 1 + Math.max(getHeight(root.left), getHeight(root.right));
}
}
__________________________________________________________________________________________________