二叉树展开为链表

二叉树展开为链表

LeetCode入口👉👉👉No.114

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

例如,给定二叉树

    1
   / \
  2   5
 / \   \
3   4   6

将其展开为:

1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          6

思路

源自LeetCode题解

  1. 将左子树插入到右子树的地方
  2. 将原来的右子树接到左子树的最右边节点
  3. 考虑新的右子树的根节点,一直重复上边的过程,直到新的右子树为 null
    1
   / \
  2   5
 / \   \
3   4   6

//将 1 的左子树插入到右子树的地方
    1
     \
      2         5
     / \         \
    3   4         6        
//将原来的右子树接到左子树的最右边节点
    1
     \
      2          
     / \          
    3   4  
         \
          5
           \
            6
            
 //将 2 的左子树插入到右子树的地方
    1
     \
      2          
       \          
        3       4  
                 \
                  5
                   \
                    6   
        
 //将原来的右子树接到左子树的最右边节点
    1
     \
      2          
       \          
        3      
         \
          4  
           \
            5
             \
              6         

python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def flatten(self, root: TreeNode) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        if not root:
            return None

        while root:
            #1.左子树空,考虑下个节点
            if not root.left:
                root = root.right
            #3.右子树接到原左子树的最右节点
            else:
                pre = root.left
                while pre.right:
                    pre = pre.right
                pre.right = root.right        
                #2.左子树插入到右子树的地方
                root.right = root.left
                root.left = None
                root = root.right

java

class Solution {
    public void flatten(TreeNode root) {
        //将左子树接到右子树 root.right = root.left
        //为了做到这一点,首先要把右子树移走,移到哪?移到左子树的右子树
        //pre = root.left;   pre.right = root.right;
        while(root != null){
            //左子树为空,直接考虑下个节点
            if(root.left == null){
                root = root.right;
            }else{
                TreeNode pre = root.left;
                while(pre.right != null){
                    pre = pre.right;
                }
                pre.right = root.right;
                root.right = root.left;
                root.left = null;
                root = root.right;
            }
        }
    }
}

使用前序遍历方法


class Solution {
    List<TreeNode> list;
    public void flatten(TreeNode root) {
        list = new ArrayList<TreeNode>();
        preorder(root);
        int size = list.size();
        for (int i = 1; i < size; i++) {
            TreeNode prev = list.get(i - 1), curr = list.get(i);
            prev.left = null;
            prev.right = curr;
        }  
    }

    void preorder(TreeNode root) {
        if (root != null) {
            list.add(root);
            preorder(root.left);
            preorder(root.right);
        }
    }
}
posted @ 2020-08-04 13:16  鱼与鱼  阅读(290)  评论(0编辑  收藏  举报