| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 3 초 | 2048 MB | 9 | 5 | 4 | 66.667% |
You are given a binary tree with $n$ nodes. The nodes are conveniently numbered from 1ドル$ to $n$. Node 1ドル$ is the root of the binary tree.
The height of the subtree rooted at node $u$ is: $$h_u = 1 + \max \left(h_\textrm{left child}, h_\textrm{right child}\right)$$ If a left or right child doesn't exist, its subtree height is defined to be 0. In particular, if a node is a leaf, it has a height of 1ドル$.
You want the tree to become height-balanced. A node is height-balanced if: $$\left|h_\textrm{left child} - h_\textrm{right child}\right| < 2$$ A binary tree is height-balanced if all its nodes are height-balanced.
Find a way to remove at most 1 leaf from the tree, such that the binary tree becomes height-balanced, or output that this is impossible. For example, the tree of the second sample input (visualized in Figure B.1) becomes balanced when removing node 5ドル$.
The input consists of:
If a left child or right child does not exist, the corresponding integer is equal to 0ドル$. It is guaranteed that the input graph is a binary tree.
Output a single integer:
balanced".impossible".4 4 2 0 0 0 0 0 3
balanced
5 2 3 0 0 0 4 0 5 0 0
5
4 2 0 3 0 4 0 0 0
impossible
Figure B.1: Visualization of Sample Input 2.