有一颗二叉树,将左子树的所有节点,转移到右子树上,如:
1 1
/ \ \
2 3 2
\
3
代码:
/// <summary>
/// 将root 中右子树->左子树,再将左子树->右子树
/// </summary>
/// <param name="root"></param>
public static void FlattenTree(TreeNode root)
{
if (root == null) return;
FlattenTree(root.Left);
FlattenTree(root.Right);
if (root.Left == null) return;
TreeNode node = root.Left;
while (node.Right != null) node = node.Right;
node.Right = root.Right;
root.Right = root.Left;
root.Left = null;
}