[英]How many Right Rotation is need to convert a tree?
I select one nice interview question. 我选择一个不错的面试问题。 Can anyone clarify it for me?
谁能为我澄清一下?
Suppose a binary tree with 6 nodes is given, such that each node has only left childs.
假设给出了一个具有6个节点的二叉树,因此每个节点只有左子节点。 With how many "right rotate" operations (without any left rotates), we can convert this tree to a tree in which each node has only right childs?
通过执行多少次“向右旋转”操作(没有任何向左旋转),我们可以将该树转换为每个节点仅具有右子节点的树吗?
my solution: 我的解决方案:
I think n-1 rotation is enough (by simulation), but I couldent make a proof, which expert could help me for proof or idea? 我认为n-1旋转就足够了(通过模拟),但是我可以做一个证明,哪位专家可以帮助我进行证明或提出想法?
The tree starts with a root
node and 5 children (which is n-1
children) to the left. 该树从
root
开始,左侧有5个子节点( n-1
个子节点)。 Each rotation around the root
increases the number of children on the right by 1. So after 5 rotations (meaning n-1
rotations) all of the children will be on the right. 围绕
root
每次旋转都会使右侧的子root
增加1。因此,在旋转5圈(即n-1
旋转)之后,所有子代都将在右侧。
Proof by induction: propose that after n
rotations there are n
children on the right. 通过归纳证明:提出
n
旋转后,右边有n
孩子。
Step 1: After 1 rotation there is 1 child on the right. 步骤1:旋转1次后,右边有1个孩子。
Step 2: Assume that after n
rotations there are n
children on the right, and prove that after n+1
rotations, there are n+1
children on the right. 步骤2:假设在
n
旋转之后,右边有n
孩子,并证明在n+1
旋转之后,右边有n+1
孩子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.