FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
coding-Interview/SerializeDeserializeBinaryTree.java at master · arjunmullick/coding-Interview · GitHub
arjunmullick
/
coding-Interview
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
coding-Interview
/
SerializeDeserializeBinaryTree.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
33 lines (30 loc) · 966 Bytes
Breadcrumbs
coding-Interview
/
SerializeDeserializeBinaryTree.java
Copy path
File metadata and controls
33 lines (30 loc) · 966 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public
class
Codec
{
// Encodes a tree to a single string.
public
String
serialize
(
TreeNode
root
) {
String
result
=
""
;
if
(
root
==
null
){
return
"null"
;
}
return
Integer
.
toString
(
root
.
val
) +
","
+
serialize
(
root
.
left
) +
","
+
serialize
(
root
.
right
);
}
// Decodes your encoded data to tree.
public
TreeNode
deserialize
(
String
data
) {
String
[]
arr
=
data
.
split
(
","
);
List
<
String
>
list
=
new
LinkedList
<
String
>(
Arrays
.
asList
(
arr
));
TreeNode
root
=
helper
(
list
);
return
root
;
}
private
TreeNode
helper
(
List
<
String
>
list
){
if
(
list
.
size
() ==
0
){
return
null
;
}
String
val
=
list
.
remove
(
0
);
if
(
val
.
equalsIgnoreCase
(
"null"
)){
return
null
;
}
TreeNode
next
=
new
TreeNode
(
Integer
.
valueOf
(
val
));
next
.
left
=
helper
(
list
);
next
.
right
=
helper
(
list
);
return
next
;
}
}
Back
|
FazBrowse Home
|
New Git URL