Classic
Clean & simple
Crystal
Glassmorphism + Neon
Cyberpunk
Neon glitch art
🚫
违规Action检测
禁止Use右键菜单
⚠️ 此Action已被系统记录
1

Rotate to root

📝 Solutions 🔵 仅限 C++
Problems Description
<p>Rotate-to-root is a heuristic for balancing binary search trees. In this problem, the contents of the tree will be ignored, and you will be asked about the effect the heuristic has on the structure of the tree.</p> <p>A binary tree is either empty or consists of a node with a left child and a right child, where each of the children are also binary trees. No node has more than one parent, and there are no cycles. In any non-empty tree, there is exactly one node without a parent, which is called the root of the tree.</p> <p>The rotate-to-root heuristic takes effect when a node in the tree, say X, is accessed. While X is not the root of the tree, the following procedure is executed:</p> <ul> <li>If X is the left child of its parent, a right tree rotation is performed. Let P be the parent of X, let A be X's left child, let B be X's right child, and let C be the right child of P. Then, P is replaced in the tree by X, so P's parent (if any) becomes X's parent; X's right child becomes P's left child (which replaces X); and P replaces X's right child. Diagram:<br /> <img width="419" height="180" src="http://plg1.cs.uwaterloo.ca/%7Eacm00/090927/Erightrot.png" alt="" /></li> <li>If X is the right child of its parent, a left tree rotation is performed. Let P be the parent of X, let A be the left child of P, let be B be X's left child, and let C be X's right child. Then, P is replaced in the tree by X, so P's parent (if any) becomes X's parent; X's left child becomes P's right child (which replaces X); and P replaces X's left child. Diagram:<br /> <img width="422" height="180" src="http://plg1.cs.uwaterloo.ca/%7Eacm00/090927/Eleftrot.png" alt="" /></li> </ul> <p>Tree rotations preserve the order of the nodes in the tree, which is why they are useful, but that is not important for this problem.</p> <p>The height of a binary tree is the longest length of a path from its root to the bottom of the tree. More formally, the height of the empty tree is 0, and the height of a non-empty tree that has a root node X with children A and B is 1 + max{height(A), height(B)}.</p> <p>Given a binary tree, you must determine, for each node X, what the height of the tree would be after X is rotated to root.</p>
Input格式
<p>Input consists of a number of test cases. The first line of each test case contains the integer <i>N</i>, the number of nodes in the binary tree, where 1 &lt;= <i>N</i> &lt;= 10<sup>5</sup>.</p> <p>The following <i>N</i> lines of input each contain two integers. The <i>i</i><sup>th</sup> pair of these integers is <i>l<sub>i</sub></i> and <i>r<sub>i</sub></i>, the left and right children of node <i>i</i>. If <i>l<sub>i</sub></i> = 0, then the left child of node <i>i</i> is the empty tree, and if <i>r<sub>i</sub></i> = 0, then the right child of node <i>i</i> is the empty tree. Otherwise, 1 &lt;= <i>l</i>, <i>r</i> &lt;= N.</p> <p>It is guaranteed that the input will describe a binary tree.</p> <p>The last test case is followed by a line containing the integer <tt>0</tt>. This final line is not a test case and should not be processed.</p>
Output格式
<p>Output consists of <i>N</i> lines for each test case, where the <i>i</i><sup>th</sup> line contains an integer giving the height of the tree after node <i>i</i> is rotated to root.</p>
Sample
Sample Input 1
4 2 3 4 0 0 0 0 0 0
Sample Output 1
3 3 4 3
Submit Code
请先Login后Submit

🤖 AI 解题助手

OpenRouter
就绪
👋 你好!我是 AI 解题助手。
我已阅读了这problems题,可以帮你points析思路、提供Code示例。
🔓 完全Free · 无需消耗 Token