zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    由于媒体16摘要天
    vim note (2)
    JEECG 什么是商业版本的功能最近添加的好友?
    图解linux启动过程
    Error: unrecognized flag -version
    基于RDP瘦客户机协议的简要说明
    Android在网络上分析获取图片(支持bmp格式)
    [React] Validate Custom React Component Props with PropTypes
    [Python] Wikipedia Crawler
    [Python] Python list slice syntax fun
  • 原文地址:https://www.cnblogs.com/NPC-assange/p/11637830.html
Copyright © 2011-2022 走看看