前缀是指什么时候应该放置根节点的内容。
给定这棵树,您可以用多种方式表示它:
-
预购:Root放在第一位(然后是left和right孩子),所以列表看起来像如下:
[41, 20, 11, 29, 32, 65, 50, 91, 72, 99]
^ -------------- ------------------
| | |
| | |-----Right sub-tree
| |
| |----Left sub-tree
|
|------ Root of the tree
在左右子树子列表中保留preorder。
-
按顺序:首先放置左子节点(如果愿意,可以进行分析),然后是根和右子节点。它看起来像这样:
[11, 20, 29, 32, 41, 50, 65, 72, 91, 99]
-------------- | ------------------
| | |
| | |------- Right sub-tree
| |
| |---- Root of the tree
|
|----- Left sub-tree
现在,列表的第一部分代表左子树,根放在后面,最后是右子树。在这里,inorder 也保存在左右子树子列表中。
中序遍历可以看成是一种从左到右的扫描。
[11, 32, 29, 20, 50, 72, 99, 91, 65, 41]
-------------- ------------------ |
| | |---- Root of the tree
| |
| |----- Right sub-tree
|
|------ Left sub-tree
和其他一样,root在最后,但左右子列表保持相同的postorder属性。
此外,还可以进行其他可能的遍历
[41, 20, 65, 11, 29, 50, 91, 32, 72, 99]
| ------ -------------- ----------
| | | |-----Level 3
| | |
| | |----- Level 2
| |
| |------ Level 1
|
|----- Level 0 (aka, the root of the tree)