树遍历问题 - 教科书问题

发布于 2025-01-19 00:31:23 字数 348 浏览 0 评论 0原文

这是一个教科书中的问题,其中提供了有关二进制树的答案。

如果邮政遍历访问二进制树的节点,以u,g,t,r,a,i的顺序存储字符值,则访问顺序是同一二进制树的遍布遍历的访问顺序?

a)i,g,u,a,a,t,r

b)r,g,u,i,i,t,a

c)g,u,i,i,t,a,r

d)无法确定

答案:c


现在问题是答案的方式。我意识到,仅靠邮政遍历不足以唯一地iD树,并且可以为任何类型的树(不订购)定义前订单和后订单遍历遍历,但不能将树一起唯一地iD。内存和邮政遍历可以。这个问题并未指定BST,因此可以有所作为。因此,我想我需要澄清答案是c而不是d的,这就是我认为的。

This is a question from a textbook with the answer provided about a binary tree.

If the postorder traversal visits the nodes of a binary tree storing character values in the order of U, G, T, R, A, I, what is the visit order for an inorder traversal of the same binary tree?

a) I, G, U, A, T, R

b) R, G, U, I, T, A

c) G, U, I, T, A, R

d) cannot be determined

Answer: c


Now the question is how is C the answer. I realize a postorder traversal alone is not enough to uniquely id a tree and a pre order and postorder traversal can be defined for any kind of tree (not inorder) but cannot uniquely id a tree together. An inorder and postorder traversal can. The question does not specifiy a BST so that can make a difference. So I guess I need clarificaton on how the answer about is C rather than D which is what I think it is.

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

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

发布评论

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

评论(1

樱花细雨 2025-01-26 00:31:23

我同意你的评价。

尽管选项 C 确实与给定的后序遍历兼容——当我们采用这棵树时:

   I                Inorder: G U I T A R
 /   \              Postorder: U G T R A I
G     A
 \   / \
  U T   R

...选项 A 与给定的后序遍历兼容,如果我们采用这棵树:

    I               Inorder: I G U A T R
     \              Postorder: U G T R A I
      A
    /   \
   G     R
    \   /
     U T

正如您正确指出的那样,单独的后序遍历并不能唯一地定义二叉树,唯一正确的答案是无法确定中序遍历(选项D)。

除非原来的问题有额外的信息,否则这是一个坏问题。

I agree with your assessment.

Although it is true that option C is compatible with the given post-order traversal -- when we take this tree:

   I                Inorder: G U I T A R
 /   \              Postorder: U G T R A I
G     A
 \   / \
  U T   R

...option A is also compatible with the given post order, if we take this tree:

    I               Inorder: I G U A T R
     \              Postorder: U G T R A I
      A
    /   \
   G     R
    \   /
     U T

As you rightly indicate, a post order traversal alone does not uniquely define a binary tree, and the only right answer is that the in-order traversal cannot be determined (option D).

Unless the original question has additional information, it is a bad question.

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