Java实现 LeetCode 114 二叉树展开为链表

114. 二叉树展开为链表

给定一个二叉树,原地将它展开为链表。

例如,给定二叉树

    1
   / \
  2   5
 / \   \
3   4   6

将其展开为:

1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          6

class Solution {
    //先将左子树拉直,再将右子树拉直,置空左子树,拼接右子树
     public void flatten(TreeNode root) {
        if(root==null)
            return ;
        flatten(root.left);
        flatten(root.right);
        TreeNode temp = root.right;
        root.right = root.left;
        root.left = null;
        while(root.right!=null)
            root = root.right;
        root.right = temp;
    }
}
发布了1230 篇原创文章 · 获赞 1万+ · 访问量 69万+

猜你喜欢

转载自blog.csdn.net/a1439775520/article/details/104387253