题目
代码
class Solution {
public void flatten(TreeNode root) {
if(root == null){
return;
}
TreeNode next = root.left
if(next != null){
while(next.right != null){ // 找到左子树的最右节点
next = next.right;
}
next.right = root.right; //将左子树最右节点的右边连到根的右结点
root.right = root.left; // 把根的左子树变为右子树
root.left = null; // 同时左子树置为null
}else{ // 左子树为null, 直接等着处理下一个右结点即可
// 什么也不做
}
flatten(root.right); // 处理下一个右结点
}
}