- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想以数组的形式返回所有节点的值,但是返回值是错误的。
type TreeNode struct {
Left *TreeNode
Right *TreeNode
Val int
}
type BinaryTree struct {
Root *TreeNode
}
func PreorderRecursion(root *TreeNode, result []int) []int {
if root == nil {
return nil
}
result = append(result, root.Val)
res1 :=PreorderRecursion(root.Left,result)
res2 :=PreorderRecursion(root.Right,result)
result = append(result,res1...)
result = append(result,res2...)
return result
}
func TestBinaryTree_PreOrder(t *testing.T) {
tree := BinaryTree{}
tree.Root = &TreeNode{Val: 1}
tree.Root.Left = &TreeNode{Val: 2}
tree.Root.Right = &TreeNode{Val: 3}
tree.Root.Left.Left = &TreeNode{Val: 4}
var result []int
result =PreorderRecursion(tree.Root,result)
fmt.Println(result,"----")
}
正确的结果应该是:1 2 4 3
但我明白了:[1 1 2 1 2 4 1 3]
最佳答案
Slices hold references to an underlying array, and if you assign one slice to another, both refer to the same array. If a function takes a slice argument, changes it makes to the elements of the slice will be visible to the caller
参见 Effective Go 中的 slice Slices
PreorderRecursion 不应获取 slice 并更改它。这是一种方法。
func PreorderRecursion(root *TreeNode) []int {
if root == nil {
return nil
}
result := append([]int{}, root.Val)
res1 := PreorderRecursion(root.Left)
res2 := PreorderRecursion(root.Right)
result = append(result, res1...)
result = append(result, res2...)
return result
}
关于golang binaryTree Preorder返回值不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53927050/
我正在尝试使用返回 IEnumerable 的 yield return 实现 Tree Traversal PreOrder private IEnumerable Preorder(Node no
我正在尝试编写一个递归函数来预先打印出这些值。但是,由于某种原因,它一直打印出与我的 inOrder 函数相同的内容。 postOrder 函数可以正常工作,但我必须为那个函数稍微不同地执行递归函数。
preorderTransaversal构建二叉搜索树的方法,有什么建议请指教。 Node constructTreeFromPreorder(int[] arr,int start,int end)
为什么DOM树是先序,深度优先遍历? 与 BFT 等其他遍历相比,这种设计选择有什么优势? 我只是在调查 DOM standard并找到了 preceding 和 following 的定义: An
要从给定的前序遍历构造 BST,如果我尝试以与预序中给定的顺序相同的顺序插入 BST,我将得到 BST。那么,我们不通过对元素进行排序或执行任何其他算法来创建有序? 是否有一个例子表明不能仅通过插入元
题目地址:https://leetcode.com/problems/binary-tree-preorder-traversal/#/descriptionopen in new window 题
我想编写一个程序,可以构建二叉搜索树并显示 “预序”、“中序”和“后序”。 第一个输入是输入系列的数量。从第二行开始,每一行代表一个串行输入,用于构建二叉搜索树。 输入: 3 9,5,6,7,1,8,
我关注了this idea还有这个C++ code在 Java 中从 PreOrder 数组重建二叉搜索树。我不是重新发明算法,而是尝试实现伪代码。我在这里需要帮助。我没有得到正确的输出。我得到的输出
我知道如果没有中序和先序/后序遍历就无法构建树。因为对于给定的(仅 Inorder/Preorder/postorder),可能会生成更多数量的树。是否有任何算法或机制可以根据给定的(仅中序/先序/后
题目地址:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/description/ 题目描述
有人可以教我如何使用 Proorder 和 Inorder 数组恢复二叉树吗?我见过一些例子(JavaScript 中没有),它们有点道理,但当我尝试编写时,递归调用永远不会返回完整的树。也很想看到解
我正在为一个类(class)使用 AVL 树。 我需要用散列来标识任何给定的树,为了构建该散列,我正在考虑查找树中所有元素的先序遍历,然后通过连接每个元素的散列来构建散列。 首先,我想确保相同的预订字
我需要一些递归方面的帮助。我正在尝试在 C# 中做一个二叉树,我想知道是否可以使用递归函数演示所有 Inorder/PostOrder 和 PreOrder 遍历。 我已经为 PreOrder 完成了
题目地址:https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 题目描述 Retu
题目地址:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/descrip
我收到了一项任务,要求我必须从二叉树的 preOder 和 inOrder 返回其 postOrder 。用纸和铅笔来做这件事很容易,但任务是开发一个自动做这件事的java函数。它必须在 java 中
我已经执行过该程序几次,但我无法真正看到问题所在。基本上,它应该从 inOrder 和 preOrder 遍历数据(作为整数数组发送)重建二叉树。它是整数的二叉树。 这是我正在使用的树: 2
morris 遍历非常适用于 O(n) 时间和 O(1) 空间的 InOrder 遍历。是否有可能仅通过更改一些东西来使用相同的算法实现 PreOrder 和 PostOrder 遍历。 最佳答案 我
当我尝试打印 BST 的级别顺序时,这个问题提示了我。 这是一个 Pre-Order Sequence: 4, 1, 2, 3, 5, 6, 7, 8 In_order Sequence : 1, 2
考虑这棵树: 7 / \ / \ / \ 1 9 /
我是一名优秀的程序员,十分优秀!