-
Notifications
You must be signed in to change notification settings - Fork 0
/
297. Serialize and Deserialize Binary Tree.java
48 lines (48 loc) · 1.6 KB
/
297. Serialize and Deserialize Binary Tree.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
else
{
Queue<TreeNode> q=new LinkedList<>();
Queue<TreeNode> s=new LinkedList<>();
for(int i=0;i<s1.length;i++)
{
if(!s1[i].equals("null"))
{
System.out.println(s1[i]);
TreeNode temp=new TreeNode(Integer.parseInt(s1[i]));
q.add(temp);
s.add(temp);
}
else
{
s.add(null);
}
}
if(!q.isEmpty())
root=q.peek();
if(!s.isEmpty())
{
s.remove();
}
while(!q.isEmpty())
{
TreeNode temp=q.remove();
// System.out.println(temp.val);
if(!s.isEmpty())
{
temp.left=s.remove();
if(temp.left!=null)
System.out.println(temp.left.val);
temp.right=s.remove();
}
}
}
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));