zoukankan      html  css  js  c++  java
  • 2叉树变双链表

    class node:
        def __init__( self , data, left, right):
            self.data = data
            self.left = left
            self.right = right
     
    def tree2list(root):
        if root == None :
            return None None
        if root.left == None and root.right == None:
            return root, root
     
        right = root.right
        head, tail = tree2list(root.left)
       
        tail.right = root
        root.left = tail
       
        head2, tail2 = tree2list(right)
        root.right = head2
        head2.left = root
     
        return head, tail2
     
    n1 = node(1, NoneNone )
    n2 = node(2, NoneNone )
    n3 = node(3, n1, n2)
    n4 = node(4, NoneNone )
    n5 = node(5, n3, n4)
     
    root = n5
    head, tail = tree2list(root)
    it = head
    while it != None:
        print it.data
        it = it.right
     
    print "—————"
     
    it = tail
    while it != None:
        print it.data
        it = it.left
        
  • 相关阅读:
    L1-021 重要的话说三遍
    L1-020 帅到没朋友
    pytest--钩子
    pytest--allure
    pytest--常用插件
    pytest--高级用法
    pytest--配置文件
    pytest--控制运行
    pytest--fixture
    pytest--使用前提
  • 原文地址:https://www.cnblogs.com/ohscar/p/3109643.html
Copyright © 2011-2022 走看看