[Data structure] Practice questions about trees (2)

Top ten questions: [Data structure] Practice questions about trees (1)

11. Knowing that the pre-order traversal sequence of a binary tree is 5 7 4 9 6 2 1, and the middle-order traversal sequence is 4 7 5 6 9 12, then the subsequent traversal sequence is ()

A.4 2 5 7 6 9 1
B.4 2 7 5 6 9 1
C.4 7 6 1 2 9 5
D.4 7 2 9 5 6 1

Answer: C
analysis:
This question is similar to the OJ question above. The root of the subtree is found through the preorder traversal, and the left and right intervals of the subtree are found according to the position of the root element in the middle order traversal. Therefore: the
root is: 5
Left subtree of 5: Right subtree of 4 7 5 : The root of the left subtree of 6 9 1 2
5 is: The root of the right subtree of 7 5 is:
The left subtree of 9 7: Right of 4 7: Left subtree of empty 9: Right subtree of 6 9: 2
So the structure of this tree is:
5
7 9
4 6 2
1
Post-order traversal: 4 7 6 1 2 9 5

12. Knowing that the middle order traversal sequence of a binary tree is JGDHKBAELIMCF, and the post-order traversal sequence is JGKHDBLMIEFCA, then the pre-order traversal sequence is ()

A.ABDGHJKCEFILM
B.ABDGJHKCEILMF
C.ABDHKGJCEILMF
D.ABDGJHKCEIMLF

Answer: B
Analysis:
Similar to the previous question, the root of the subtree is determined according to the post-order traversal. The post-order traversal looks from the back to the front, and the last element is the root, which is just the opposite of the pre-order traversal. , Should be the root, right, and left, and determine the left and right intervals of the subtree according to the middle order traversal.
Therefore: the
root is: A
Left subtree of A: JGDHKB A right subtree: ELIMCF
A’s left subtree root: BA’s right subtree root: C
B’s left subtree: JGDHK B’s right subtree: Left subtree of empty C: Right subtree of ELIM C: Root of left subtree of FB:
Root of left subtree of DC: Root of left subtree of E
D: Root of right subtree of GD: Right of HE The root of the subtree: I
so the structure of the tree is:
A
BC
DEF
GHI
JKLM
so the preorder traversal:
ABDGJHKCEILMF

13. Knowing that the pre-order traversal sequence of a binary tree is ABDEC and the middle-order traversal sequence is BDEAC, then the binary tree ()

A. It is a full binary tree
B. It is a full binary tree, not a full binary tree
C. Not a full binary tree
D. It is a binary tree with no right subtree at all nodes

Answer: C
analysis:
The structure of the tree can be determined according to the idea of ​​question 11:
A
B c
D
E
before: ABDEC: BDEAC
so it is neither a full binary tree nor a complete binary tree

14. The pre-order traversal sequence and the post-order traversal sequence of a non-empty binary tree are exactly the opposite, then the binary tree must satisfy ()

A. All nodes have no left children
B. All nodes have no right children
C. Only one leaf node
D. At most only one node

Answer: C
analysis:
If the preorder traversal and the postorder traversal sequence are exactly the opposite, it means that it is a one-sided tree. For example, the structure of the tree formed by the following preorder and inorder sequences:
12345
54321 The
first type: the second type :
              1 1
        2 2
    3 3
  4 4
5 5
For a unilateral tree, there is only one leaf node.

15. Suppose a certain binary tree has the following characteristics: each node is either a leaf node or has 2 subtrees. If there are m (m>0) leaf nodes in such a binary tree, then the total number of nodes on the binary tree is ()

A.2m+1
B.2(m-1)
C.2m-1
D.2m

Answer: C
analysis:
According to the meaning of the question, this tree is a complete binary tree, so there are only nodes with degree 0 and degree 2.
Set the total number of nodes as N and the number of nodes with degree i as Ni,
then a complete binary tree: N = N0 + N2 (1)
According to the relationship between the degree and the edge, the complete binary tree can be obtained:
N-1 = 2 * N2 ie: N = 2 * N2 + 1 (2)
It can be obtained from (1) and (2) : N2 = N0-1
and the question tells N0 = m, so N = m + m-1

16. Suppose the depth of the root node is 1, then the depth of a binary tree with n nodes must be in the interval ()

A. [logn + 1 , n]
B. [logn , n]
C. [logn + 1 , n - 1]
D. [logn + 1 , n + 1]

Answer: A
resolution:
Maximum depth: This tree is a one-sided tree, and the depth is n.
Minimum depth: This tree is a complete binary tree. If it is a complete binary tree, assuming the height is h,
then the first h-1 level is a full binary tree, so n Satisfy:
2^(h-1)-1 <n <= 2^h-1
That is:
2^(h-1)< n + 1 <= 2^h
Take the logarithm of both sides to get:
h-1 <log( n + 1) <= h
so:
log(n + 1) <= h <log(n + 1) + 1
log(n + 1) <= h <log(2(n + 1)) //logarithm The property log(ab) = log(a) + log(b) is
compared with the item options: only A and B are optional, but the lower bound log(n) of the B option is less than the lower bound log(n + 1) of h, only The interval of option A is consistent. Option A has a lower bound: logn + 1 = log(2n)

17. For any binary tree, let N0, N1, and N2 be the number of nodes with degrees of 0, 1, and 2, respectively, then the following formula must be correct ()

A.N0 = N2 + 1
B.N1 = N0 + 1
C.N2 = N0 + 1
D.N2 = N1 + 1

Answer:
Analysis of A :
Total number of nodes N: N = N0 + N1 + N2
The relationship between degree and edge: N-1 = 0 * N0 + 1 * N1 + 2 * N2 The
above two formulas can be derived: N0 + N1 + N2 -1 = N1 + 2 * N2
can be obtained: N0 = N2 + 1

18. In the post-order non-recursive traversal of the binary tree, the extra space required includes ()

A. A stack
B. A queue
C. A stack and a record mark sequence table
D. A queue and a record mark sequence table

Answer: C
analysis: It
requires a stack to simulate the recursive process, and a sequence table to store nodes.

19. Binary tree () traversal is equivalent to breadth-first traversal, () traversal is equivalent to depth-first traversal

A. Presequence middle sequence
B. Middle sequence presequence
C. Sequence post sequence
D. Sequence presequence

Answer: D
analysis:
Breadth first needs to traverse all the possible positions in the next step before a deeper traversal can be carried out. Layer sequence traversal is a breadth first traversal.
Depth-first is to traverse a complete path (the complete path from root to leaf) before returning to the upper layer, and then traverse the next path. Pre-order traversal is a kind of depth-first traversal.

20. If the result of the preorder traversal of a binary tree ABCD, then the condition of the binary tree have different () species
A.13
B.14
C.15
D.16

Answer: B
analysis:
First, the height of this binary tree must be between 3 and 4 levels:
three levels:
A(B(C,D),()), A((),B(C,D)), A (B(C,()),D), A(B((),C),D),
A(B,C(D,())), A(B,C((),D))
four:
If four is unilateral tree, each layer only one node, except the root node through other nodes have two options on the left or right upper node, so the two 2 2 of 8 kinds
for a total of 14 kinds.

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/112425804