简体   繁体   English

在按顺序存储在数组中的二叉树中找到根节点

[英]locate root node in binary tree stored in array in inorder

Example Tree 示例树

2 -> root
1 -> Left
3 -> right 

stored in array in in order [1, 2, 3] 以[1、2、3]的顺序存储在数组中

How to retrieve the root node knowing that the tree is stored in inorder? 如何在知道树按顺序存储的情况下检索根节点?

According to me all three as possible candidates of root node. 据我说,这三个都是根节点的候选对象。

Indeed all 3 are possible candidates. 实际上,所有3个都是可能的候选者。

Here are possible trees that would result in the given in-order traversal: 以下是可能导致给定顺序遍历的树:

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

An in-order traversal isn't necessarily sufficient to uniquely identify a tree (and thus consistently identify the root). 有序遍历不一定足以唯一地识别树(并因此一致地识别根)。 Assuming unique tree elements, you need either a pre-order or post-order traversal paired with an in-order traversal. 假设唯一的树元素,您需要将有序遍历与前序遍历或后序遍历配对。

Reference - Which combinations of pre-, post- and in-order sequentialisation are unique? 参考- 前顺序,后顺序和顺序顺序的哪些组合是唯一的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM