← 回題庫

卷別

資料結構與演算法

選擇

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

考科演算法

(5 points) We have a binary tree TT and for every node v∈Tv \in T, we want to compute the new height if we select vv as the new root.

We consider the effect of making vv the new root. First, the height of the subtree of vv is still l(v)l(v). Second, since vv is now the new root, it could go up to its parent and then to the farthest leaf. We define this distance as r(v)r(v).

  • (A)
    The new tree after selecting a new root is always a binary tree.
  • (B)
    The height of the tree after making vv the new root is max⁡(l(v),r(v))\max(l(v), r(v)).
  • (C)
    We can compute r(v)r(v) for all tree nodes vv's in a top-down manner. We assume that the rr value of vv's parent in TT is known, so vv only needs to consider the rr value of its parent and the ll value of its sibling in TT, in order to compute its own rr.
  • (D)
    The rr value of the root of TT is 1.
  • (E)
    We can compute the rr values for all nodes in O(n)O(n) time.

標準解析

AI 已複查