• LeetCode--114--二叉树展开为链表(python)


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

    例如,给定二叉树

      1
      /
       2   5
      /    
     3 4      6
    将其展开为:

        1
         
          2
           
            3
           
               4
            
           5
             
            6

    将root的右子树放到root的左子树的最右边作为右孩子         将root的左孩子变为自己的右孩子 (root.left=None)           root = root.right

      1                                  1                                         1       
      /                               /                                             
       2   5                           2                                               2
      /                             /                                               /  
     3 4      6                     3  4                                             3  4   
                                                                                               
                                              5                                                 5
                                                                                                       
                                                 6                                                  6                    

     1 class Solution:
     2     def flatten(self, root: TreeNode) -> None:
     3         """
     4         Do not return anything, modify root in-place instead.
     5         """
     6         if root==None or root.left == None and root.right == None :
     7             return root
     8         while(root != None):
     9             if(root.left == None):
    10                 root = root.right
    11             else:
    12                 pre = root.left
    13                 while pre and pre.right!=None:
    14                     pre = pre.right
    15              16                 pre.right = root.right
    17                 root.right = root.left
    18                 root.left = None
    19                 root = root.right
  • 相关阅读:
    linux查看系统挂载磁盘
    Jenkins REST API 实例
    Python中 __init__.py的作用
    转载:Python中打开文件的方式(With open)
    利用tail -f /dev/null命令防止container启动后退出
    echo -e 命令详解
    Python排序
    Linux操作系统下删除除具体文件或目录之外的文件
    linux pam模块学习
    vsftpd服务器配置
  • 原文地址:https://www.cnblogs.com/NPC-assange/p/11637830.html
Copyright © 2020-2023  润新知