【发布时间】:2016-07-04 17:13:56
【问题描述】:
树结构如下:
type TreeData struct {
Name string `json:"name"`
Depth int `json:"depth"`
Children []TreeData `json:"children"`
}
我有两棵树,我想把它们合并成一棵树。
我该怎么做?
如果有人可以向我展示您的代码,我将不胜感激!
不知道能不能用递归的方式来完成合并?
树一:
{
"name": ".",
"depth": 1,
"children": [
{
"name": "com",
"depth": 2,
"children": [
{
"name": "didi"
"depth": 3,
"children": [
{
"name": "dev",
"depth": 4,
"children": null
}
]
}
]
}
]
}
树二:
{
"name": ".",
"depth": 1,
"children": [
{
"name": "com",
"depth": 2,
"children": [
{
"name": "didi"
"depth": 3,
"children": [
{
"name": "influxdb",
"depth": 4,
"children": [
{
"name": "cluster-one"
"depth": 5
"children": null
}
]
}
]
}
]
}
]
}
合并:
{
"name": ".",
"depth": 1,
"children": [
{
"name": "com",
"depth": 2,
"children": [
{
"name": "didi"
"depth": 3,
"children": [
{
"name": "influxdb",
"depth": 4,
"children": [
{
"name": "cluster-one"
"depth": 5
"children": null
}
]
},
{
"name": "dev",
"depth": 4,
"children": null
}
]
}
]
}
]
}
【问题讨论】:
-
先给我们看一些代码。
-
我不知道该怎么做......