Binary tree traversal
Title description
(Single choice) Given a binary tree, if the node order of the first-order traversal is: KDCEFGHB, the middle-order traversal is: CDFEGHKB, then the result of the subsequent traversal is ()
- [ ] A. CFHGEBDK
- [ ] B. CDFEGHBK
- [ ] C. FGHCDEBK
- [x] D. CFHGEDBK
Tree traversal rules: (remember by root position)
- First order: (root left and right) root first
- Middle order: (left root right) root middle
- Post sequence: (left and right root) root last
Ideas:
- First determine the root node
- In determining the left subtree and the right subtree
2.1 Find the root of the left subtree from the left subtree, and go back to step 1
2.2 Find the root of the right subtree from the right subtree, go back to step 1 - Until the search is completed, build the tree
Detailed analysis:
The first traversal in pre-order must be the root of the tree, then K is the root;
Then the two sides of K in the middle-order traversal are the left subtree and the right subtree respectively;
The left subtree is CDFEGH and the right subtree is B
K
CDFEGH B
The first order traversal in the left subtree is that D is the first in DCEFGH, and all D is the root of the left subtree;
C is the left subtree, FEGH is the right subtree
K
D B
C FEGH
In the FEGH subtree, the pre-order traversal is EFGH, and all E is the root of the subtree
F is the left subtree, GH is the right subtree
K
D B
C E
F GH
In the GH subtree, GH is traversed first, so G is the root and H is the right subtree
K
D B
C E
F G
H
So the subsequent traversal is:
C F H G E D B K
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。