将左子树移到右边循环即可
1 public class Solution { 2 public void flatten(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 while(root != null){ 6 if(root.left != null){ 7 TreeNode right = root.right; 8 TreeNode left = root.left; 9 root.left = null; 10 root.right = left; 11 while(left.right!=null) 12 left = left.right; 13 left.right = right; 14 } 15 root = root.right; 16 } 17 } 18 19 }