Why Nostr? What is Njump?
2023-02-10 07:56:58
in reply to

dave on Nostr: ```go type Tree struct { val int left *Tree right *Tree } func ArrToTree(arr []int) ...

```go
type Tree struct {
val int
left *Tree
right *Tree
}


func ArrToTree(arr []int) *Tree {
// 创建根节点
root := &Tree{}
// 节点队列
nodeQueue := []*Tree{}
// 入队
nodeQueue = append(nodeQueue, root)
// 当前节点索引
index := 0
// 处理节点
for len(nodeQueue) > 0 && index < len(arr) {
// 取出节点
node := nodeQueue[0]
nodeQueue = nodeQueue[1:]
// 节点赋值
node.val = arr[index]
// 左右节点入队
if (2*index + 1) < len(arr) {
leftNode := &Tree{}
node.left = leftNode
nodeQueue = append(nodeQueue, leftNode)
}
if (2*index + 2) < len(arr) {
rightNode := &Tree{}
node.right = rightNode
nodeQueue = append(nodeQueue, rightNode)
}
// 下一个节点
index++
}
return root
}
```
Author Public Key
npub1tsgw6pncspg4d5u778hk63s3pls70evs4czfsmx0fzap9xwt203qtkhtk4