JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

将左子树移到右边循环即可

 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 }

 

posted on 2013-11-11 15:39  JasonChang  阅读(159)  评论(0编辑  收藏  举报