← 回題庫

卷別

資料結構與演算法

選擇

國立臺灣大學 2024 · 考試 · 第 8 題

考科演算法

(5 points) We define the height of a tree to be the maximum number of edges from the root to a leaf. We also define the level of a node vv, denoted as l(v)l(v), to be the height of the subtree rooted at vv. Which of the following descriptions is correct?

  • (A)
    If vv is a leaf, then l(v)=1l(v) = 1.
  • (B)
    If vv is an internal node, l(v)=max⁡s∈Sl(s)l(v) = \max_{s \in S} l(s), where SS is the set of children of vv.
  • (C)
    We can compute l(v)l(v) by starting a breadth-first-search (BFS) from vv, traversing all nodes of the subtree rooted at vv, and computing the maximum distance to a leaf as l(v)l(v). Since there are no more than nnvv's and each BFS takes O(n)O(n) time, the total time complexity is O(n2)O(n^2).
  • (D)
    We can compute the levels for all nodes by a depth-first-search (DFS) starting from the root of TT. That is, we can compute the level of an internal node vv after we compute the ll values of all children of vv.
  • (E)
    Since there are nn tree nodes to visit, the DFS from the root takes O(n)O(n) time.

標準解析

AI 已複查