---
id: 1223
lang: en
translation_of: index.md
source_sha256: 8dec8ef098a5ee4c17654c4f885fa5ecc54763cd7f5a1e21ad8066a40961e530
title: "P3830: [SHOI2012] Random Tree — Solution"
description: "The first question is fairly easy: let the state f[i] be the expected depth when there are i leaf nodes."
tags: ["OI","DP","Probability and Expectation"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "有i个叶子节点深度大于等于j的期望树高 is rendered literally as “the expected tree height with i leaf nodes and depth greater than or equal to j”, keeping the author's wording."
---

## Approach

### Question 1

The first question is fairly easy. Let the state $f[i]$ be the expected depth when there are $i$ leaf nodes. Considering that a split from the previous state contributes an extra $f[i-1]+2$ to the total depth, we know:

$$
f[i] = \frac{f[i-1] * (i-1) + f[i-1] + 2}{i} = f[i-1] + \frac{2}{i}
$$

<!--more-->

### Question 2

The second question is a lot harder. The key is to set the state $f[i][j]$: the expected tree height with $i$ leaf nodes and depth greater than or equal to $j$. Consider enumerating a $k$ as the number of leaf nodes in the left subtree, and compute:

$$
f[i][j] = \sum_{k = 1}^{i - 1} f[k][j - 1] + f[i - k][j - 1] - f[k][j - 1] * f[i - k][j - 1]
$$

The last part removes what was counted twice. The answer is $\sum_{i = 1}^{n - 1} f[n][i]$.
