C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
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 ser, deser;
// TreeNode* ans = deser.deserialize(ser.serialize(root));
运行代码
提交
golang 解法, 执行用时: 8 ms, 内存消耗: 7.6 MB, 提交时间: 2022-11-23 17:15:53
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type Codec struct{}
func Constructor() (_ Codec) {
return
}
func (c Codec) serialize(root *TreeNode) string {
if root == nil {
return "X"
}
left := "(" + c.serialize(root.Left) + ")"
right := "(" + c.serialize(root.Right) + ")"
return left + strconv.Itoa(root.Val) + right
}
func (Codec) deserialize(data string) *TreeNode {
var parse func() *TreeNode
parse = func() *TreeNode {
if data[0] == 'X' {
data = data[1:]
return nil
}
node := &TreeNode{}
data = data[1:] // 跳过左括号
node.Left = parse()
data = data[1:] // 跳过右括号
i := 0
for data[i] == '-' || '0' <= data[i] && data[i] <= '9' {
i++
}
node.Val, _ = strconv.Atoi(data[:i])
data = data[i:]
data = data[1:] // 跳过左括号
node.Right = parse()
data = data[1:] // 跳过右括号
return node
}
return parse()
}
/**
* Your Codec object will be instantiated and called as such:
* ser := Constructor();
* deser := Constructor();
* data := ser.serialize(root);
* ans := deser.deserialize(data);
*/
golang 解法, 执行用时: 8 ms, 内存消耗: 6.3 MB, 提交时间: 2022-11-23 17:15:04
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type Codec struct{}
func Constructor() (_ Codec) {
return
}
func (Codec) serialize(root *TreeNode) string {
sb := &strings.Builder{}
var dfs func(*TreeNode)
dfs = func(node *TreeNode) {
if node == nil {
sb.WriteString("null,")
return
}
sb.WriteString(strconv.Itoa(node.Val))
sb.WriteByte(',')
dfs(node.Left)
dfs(node.Right)
}
dfs(root)
return sb.String()
}
func (Codec) deserialize(data string) *TreeNode {
sp := strings.Split(data, ",")
var build func() *TreeNode
build = func() *TreeNode {
if sp[0] == "null" {
sp = sp[1:]
return nil
}
val, _ := strconv.Atoi(sp[0])
sp = sp[1:]
return &TreeNode{val, build(), build()}
}
return build()
}
/**
* Your Codec object will be instantiated and called as such:
* ser := Constructor();
* deser := Constructor();
* data := ser.serialize(root);
* ans := deser.deserialize(data);
*/