C++
Java
Python
Python3
C
C#
JavaScript
Ruby
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
运行代码
提交
javascript 解法, 执行用时: 112 ms, 内存消耗: 54.2 MB, 提交时间: 2022-11-23 17:17:55
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
var serialize = function(root) {
if (root == null) {
return "X";
}
const left = "(" + serialize(root.left) + ")";
const right = "(" + serialize(root.right) + ")";
return left + root.val + right;
};
var deserialize = function(data) {
const ptr = [0];
return parse(data, ptr);
};
const parse = (data, ptr) => {
if (data[ptr[0]] === 'X') {
++ptr[0];
return null;
}
let cur = new TreeNode(0);
cur.left = parseSubtree(data, ptr);
cur.val = parseInt(data, ptr);
cur.right = parseSubtree(data, ptr);
return cur;
}
const parseSubtree = (data, ptr) => {
++ptr[0]; // 跳过左括号
const subtree = parse(data, ptr);
++ptr[0]; // 跳过右括号
return subtree;
}
const parseInt = (data, ptr) => {
let x = 0, sgn = 1;
if (isNaN(Number(data[ptr[0]]))) {
sgn = -1;
++ptr[0];
}
while (!isNaN(Number(data[ptr[0]]))) {
x = x * 10 + data[ptr[0]++].charCodeAt() - '0'.charCodeAt();
}
return x * sgn;
}
/**
* Your functions will be called as such:
* deserialize(serialize(root));
*/
javascript 解法, 执行用时: 896 ms, 内存消耗: 51.5 MB, 提交时间: 2022-11-23 17:17:20
/**
* 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}
*/
var serialize = function(root) {
return rserialize(root, '');
};
/**
* Decodes your encoded data to tree.
*
* @param {string} data
* @return {TreeNode}
*/
var deserialize = function(data) {
const dataArray = data.split(",");
return rdeserialize(dataArray);
};
const rserialize = (root, str) => {
if (root === null) {
str += "None,";
} else {
str += root.val + '' + ",";
str = rserialize(root.left, str);
str = rserialize(root.right, str);
}
return str;
}
const rdeserialize = (dataList) => {
if (dataList[0] === "None") {
dataList.shift();
return null;
}
const root = new TreeNode(parseInt(dataList[0]));
dataList.shift();
root.left = rdeserialize(dataList);
root.right = rdeserialize(dataList);
return root;
}
/**
* Your functions will be called as such:
* deserialize(serialize(root));
*/