如何绘制此输入的二叉树:[1,2,2,3,null,null,3,4,null,null,4]?

发布于 2025-01-13 09:13:13 字数 193 浏览 2 评论 0 原文

这是我画的

我显然遗漏了一些东西。如何包含数组索引 10 处的值? 我将感谢您的帮助。

This is what I have drawnenter image description here

I am apparently missing something. How to include the value at index 10 of the array?
I will be grateful for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2025-01-20 09:13:13

数组编码不能让您假设节点的子节点位于索引 i*2+1i*2+2 处。如果编码树是完整二叉树,则情况如此,但如果不是,则不能使用此公式。

相反,您应该在构建树时跟踪树的底层,仅注册真实节点(而不是null)。然后将下一个子节点分布在(当时)底层的节点中,等等。这实际上是一种广度优先的遍历方法。

过程如下:

创建一个队列,并为输入列表中的第一个值创建一个节点(如果列表不为空),并将其入队。

然后,只要有更多输入需要处理,就重复此操作:

  • 将节点从队列中出列,
  • 从输入中读取接下来的两个值,并为它们创建节点。如果输入中没有足够的剩余值,请改用 null
  • 将这两个节点作为子节点附加到您从队列中获取的节点
  • 。那些不为 null 的子节点应在队列中排队。

如果将此算法应用于示例输入 [1,2,2,3,null,null,3,4,null,null,4],那么我们首先得到根,并将其放入队列中。因此,就在循环开始之前,我们有:

        root: 1        queue = [1]
                       remaining input = [2,2,3,null,null,3,4,null,null,4]

我在这里用数字描述了队列内容,但它们实际上是节点实例。

在循环的第一次迭代之后,我们从输入中读取 2 和 2,为它们创建节点,将它们附加到出队节点,并将这些子节点入队,我们得到:

        root: 1        queue = [2, 2]
             / \       remaining input = [3,null,null,3,4,null,null,4]
            2   2

在迭代 #2 之后(请注意,没有 null 入队) ):

        root: 1        queue = [2, 3]
             / \       remaining input = [null,3,4,null,null,4]
            2   2
           / *
          3    

迭代#3之后:

        root: 1        queue = [3, 3]
             / \       remaining input = [4,null,null,4]
            2   2
           / * * \
          3       3

迭代#5之后:

        root: 1        queue = [3, 4]
             / \       remaining input = [null,4]
            2   2
           / * * \
          3       3
         / *
        4

最终迭代之后:

        root: 1        queue = [4, 4]
             / \       remaining input = []
            2   2
           / * * \
          3       3
         / *     * \
        4           4

队列不为空,但由于没有更多输入,这些排队的节点代表不需要进一步处理的叶子。

The array encoding is not such that you can assume that a node's children are at the index i*2+1 and i*2+2. That would be true if the encoded tree were a complete binary tree, but when it is not, you cannot use this formula.

Instead, you should keep track of the bottom layer of the tree as you build it, only registering real nodes (not null). Then distribute the next children among the nodes in the (then) bottom layer, etc. This is really a breadth-first traversal method.

This is the procedure:

Create a queue, and create a node for the first value in the input list (if the list is not empty), and enqueue it.

Then repeat for as long as there is more input to process:

  • dequeue a node from the queue
  • read the next two values from the input, and create nodes for them. If there are not enough values remaining in the input, use null instead.
  • Attach these two nodes as children to the node that you had taken from the queue
  • Those children that were not null should be enqueued on the queue.

If you apply this algorithm to the example input [1,2,2,3,null,null,3,4,null,null,4], then we get first the root, which is put on the queue. So just before the loop starts we have:

        root: 1        queue = [1]
                       remaining input = [2,2,3,null,null,3,4,null,null,4]

I depict here the queue contents with numbers, but they really are node instances.

After the first iteration of the loop, in which we read 2 and 2 from the input, create nodes for them, attach them to the dequeued node, and enqueue those children, we get:

        root: 1        queue = [2, 2]
             / \       remaining input = [3,null,null,3,4,null,null,4]
            2   2

After iteration #2 (note that no null is enqueued):

        root: 1        queue = [2, 3]
             / \       remaining input = [null,3,4,null,null,4]
            2   2
           / *
          3    

After iteration #3:

        root: 1        queue = [3, 3]
             / \       remaining input = [4,null,null,4]
            2   2
           / * * \
          3       3

After iteration #5:

        root: 1        queue = [3, 4]
             / \       remaining input = [null,4]
            2   2
           / * * \
          3       3
         / *
        4

After the final iteration:

        root: 1        queue = [4, 4]
             / \       remaining input = []
            2   2
           / * * \
          3       3
         / *     * \
        4           4

The queue is not empty, but as there is no more input, those queued nodes represent leaves that need no further processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文