卷別
資料結構與演算法
選擇國立臺灣大學 2024 · 考試 · 第 9 題
考科演算法
(5 points) We have a binary tree and for every node , we want to compute the new height if we select as the new root.
We consider the effect of making the new root. First, the height of the subtree of is still . Second, since is now the new root, it could go up to its parent and then to the farthest leaf. We define this distance as .
- (A)The new tree after selecting a new root is always a binary tree.
- (B)The height of the tree after making the new root is .
- (C)We can compute for all tree nodes 's in a top-down manner. We assume that the value of 's parent in is known, so only needs to consider the value of its parent and the value of its sibling in , in order to compute its own .
- (D)The value of the root of is 1.
- (E)We can compute the values for all nodes in time.