-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path297.二叉树的序列化与反序列化.js
88 lines (76 loc) · 1.63 KB
/
297.二叉树的序列化与反序列化.js
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
/*
* @lc app=leetcode.cn id=297 lang=javascript
*
* [297] 二叉树的序列化与反序列化
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* Encodes a tree to a single string.
*
* @param {TreeNode} root
* @return {string}
*/
const NULL = 'NULL'
var serialize = function(root) {
let res = []
serializeHelper(root, res)
return res.join(',')
}
function serializeHelper(root, res) {
// base case
if (!root) {
res.push(NULL)
return
}
res.push(root.val)
serializeHelper(root.left, res)
serializeHelper(root.right, res)
return res
}
const node = {
val: 1,
left: { val: 2, left: { val: 3, left: null, right: null }, right: null },
right: { val: 4, left: { val: 5, left: null, right: null }, right: null }
}
let res = serialize(node)
console.log('res', res)
/**
* Decodes your encoded data to tree.
*
* @param {string} data
* @return {TreeNode}
*/
var deserialize = function(dataStr) {
const nodeList = dataStr.split(',')
return deserializeHelper(nodeList)
}
function deserializeHelper(nodeList) {
if (!nodeList.length) return null
const rootVal = nodeList.shift()
if (rootVal === 'NULL') {
return null
}
const root = new TreeNode(rootVal)
root.left = deserializeHelper(nodeList)
root.right = deserializeHelper(nodeList)
return root
}
function TreeNode(val) {
this.val = val
this.left = null
this.right = null
}
let res2 = deserialize(res)
console.log('res2', res2)
/**
* Your functions will be called as such:
* deserialize(serialize(root));
*/
// @lc code=end