-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRecoverATreeFromPreorderTraversal.java
113 lines (110 loc) · 3.27 KB
/
RecoverATreeFromPreorderTraversal.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
HashMap<Integer,TreeSet<Integer>> depthDashMap;
int n;
public TreeNode recoverFromPreorder(String traversal) {
depthDashMap = new HashMap<Integer,TreeSet<Integer>>();
int i, count = 0;
n = traversal.length();
char[] preorder = traversal.toCharArray();
for (i = 0; i < n; ++i)
{
if (preorder[i] == '-') ++count;
else
{
if (!depthDashMap.containsKey(count))
depthDashMap.put(count,new TreeSet<Integer>());
depthDashMap.get(count).add(i-count);
count = 0;
while (i < n && preorder[i] != '-') ++i;
--i;
}
}
return buildTree(preorder,0,0,n-1);
}
private TreeNode buildTree(char[] preorder, int currDepth, int start, int end)
{
if (start > end) return null;
int index = start, value = 0;
while (preorder[index] == '-') ++index;
while (index < n && preorder[index] >= '0' && preorder[index] <= '9')
{
value = value*10+(preorder[index]-'0');
++index;
}
TreeNode root = new TreeNode(value);
int nextDepth = currDepth+1;
int leftLimit = end;
if (depthDashMap.containsKey(nextDepth) && depthDashMap.get(nextDepth).higher(index+1) != null)
leftLimit = Math.min(leftLimit,depthDashMap.get(nextDepth).higher(index+1)-1);
root.left = buildTree(preorder,nextDepth,index,leftLimit);
root.right = buildTree(preorder,nextDepth,leftLimit+1,end);
return root;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int n, i;
char[] preorder;
public TreeNode recoverFromPreorder(String traversal) {
n = traversal.length();
i = 0;
preorder = traversal.toCharArray();
return buildTree(0);
}
private TreeNode buildTree(int depth)
{
if (i >= n) return null;
int count = 0;
while (preorder[i] == '-')
{
++count;
++i;
}
if (count < depth)
{
i -= count;
return null;
}
int value = 0;
while (i < n && preorder[i] >= '0' && preorder[i] <= '9')
{
value = value*10+(preorder[i]-'0');
++i;
}
TreeNode root = new TreeNode(value);
root.left = buildTree(depth+1);
if (root.left == null) return root;
root.right = buildTree(depth+1);
return root;
}
}