go - 如何在for循环中使用两个initStmt?

标签 go tree

Initstmt在循环中
目标是在下面的代码中添加两棵树并返回一棵新树:

/*
        3                               3
      /   \                           /   \
     /     \                         /     \
    1       2                       1       2
          /   \                            /
         /     \                          /
        1       1                        1

*/

/*
    1) Tree has root label and list of branches
    2) Each branch is again a tree
*/

type Tree struct {
    rootLabel int
    branches  []Tree
}

func addTrees(t1 Tree, t2 Tree) Tree {
    if isLeaf(t1) {
        return Tree{}
    }
    firstTreeBranches := branches(t1)
    secondTreeBranches := branches(t2)
    for branch1:= firstTreeBranches[0], branch2:= secondTreeBranches[0];;{
            
    }
}

func branches(t Tree) []Tree {
    return t.branches
}

我想进行递归调用 addTrees(branch1,branch2)在上面的代码中。
如何使用 for-loop 语法从两棵树中获取分支?

最佳答案

您可以使用以下方法实现此目的:

for branch1, branch2 := firstTreeBranches[0], secondTreeBranches[0] ; ; {
    ...
}
Try it on the playground .引用 Variable declaration了解更多信息。

关于go - 如何在for循环中使用两个initStmt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63542065/

相关文章:

performance - 代码顺序和性能

php - 如何递归构建树深度未知的 <select>

python - 使用 TF 再训练模型

unit-testing - 我如何在测试中模拟 Go 函数?

go - 使用 golang 对每个请求的上下文超时实现

json - 如何从Golang中具有json对象列表的文件中读取单个json对象

java - 二叉搜索树 : Display contents of the tree in a tree like fashion (recursive)

java - 排序 "plain-list-tree-structure"的算法

python - 二叉树获取每个级别的所有节点

Java - 具有多个节点的树数据结构 - 如何有效地搜索