小白專場-樹的同構-python語言實現

更新、更全的《數據結構與算法》的更新網站,更有python、go、人工智能教學等着你:<http://www.javashuo.com/article/p-zfinzipt-hh.htmlnode

1、題意理解

給定兩棵樹T1和T2。若是T1能夠經過若干次左右孩子互換就變成T2,則咱們稱兩棵樹是「同構的」。現給定兩棵樹,請你判斷它們是不是同構的。python

輸入格式:輸入給出2棵二叉樹的信息:算法

  • 先在一行中給出該樹的結點樹,隨後N行數據結構

  • 第i行對應編號第i個結點,給出該結點中存儲的字母、其左孩子結點的編號、右孩子結點的編號
  • 若是孩子結點爲空,則在相應位置給出「-」app

以下圖所示,有多種表示的方式,咱們列出如下兩種:網站

2、求解思路

搜到一篇也是講這個的,可是那篇並無徹底用到單向鏈表的方法,因此研究了一下,寫了一個是徹底用單向鏈表的方法:人工智能

其實應該有更優雅的刪除整個單向列表的方法,好比頭設爲none,可能會改進下?spa

# python語言實現

L1 = list(map(int, input().split()))
L2 = list(map(int, input().split()))


# 節點
class Node:
    def __init__(self, coef, exp):
        self.coef = coef
        self.exp = exp
        self.next = None


# 單鏈表
class List:
    def __init__(self, node=None):
        self.__head = node

    # 爲了訪問私有類
    def gethead(self):
        return self.__head

    def travel(self):
        cur1 = self.__head
        cur2 = self.__head
        if cur1.next != None:
            cur1 = cur1.next
        else:
            print(cur2.coef, cur2.exp, end="")
            return
        while cur1.next != None:
            print(cur2.coef, cur2.exp, end=" ")
            cur1 = cur1.next
            cur2 = cur2.next

        print(cur2.coef, cur2.exp, end=" ")
        cur2 = cur2.next
        print(cur2.coef, cur2.exp, end="")

    # add item in the tail
    def append(self, coef, exp):
        node = Node(coef, exp)
        if self.__head == None:
            self.__head = node
        else:
            cur = self.__head
            while cur.next != None:
                cur = cur.next
            cur.next = node


def addl(l1, l2):
    p1 = l1.gethead()
    p2 = l2.gethead()
    l3 = List()
    while (p1 is not None) & (p2 is not None):
        if (p1.exp > p2.exp):
            l3.append(p1.coef, p1.exp)
            p1 = p1.next
        elif (p1.exp < p2.exp):
            l3.append(p2.coef, p2.exp)
            p2 = p2.next
        else:
            if (p1.coef + p2.coef == 0):
                p1 = p1.next
                p2 = p2.next
            else:
                l3.append(p2.coef + p1.coef, p1.exp)
                p2 = p2.next
                p1 = p1.next
    while p1 is not None:
        l3.append(p1.coef, p1.exp)
        p1 = p1.next
    while p2 is not None:
        l3.append(p2.coef, p2.exp)
        p2 = p2.next
    if l3.gethead() == None:
        l3.append(0, 0)
    return l3


def mull(l1, l2):
    p1 = l1.gethead()
    p2 = l2.gethead()
    l3 = List()
    l4 = List()
    if (p1 is not None) & (p2 is not None):
        while p1 is not None:
            while p2 is not None:
                l4.append(p1.coef * p2.coef, p1.exp + p2.exp)
                p2 = p2.next
            l3 = addl(l3, l4)
            l4 = List()
            p2 = l2.gethead()
            p1 = p1.next
    else:
        l3.append(0, 0)
    return l3


def L2l(L):
    l = List()
    L.pop(0)
    for i in range(0, len(L), 2):
        l.append(L[i], L[i + 1])
    return l


l1 = L2l(L1)
l2 = L2l(L2)
l3 = List()
l3 = mull(l1, l2)
l3.travel()
print("")
l3 = List()
l3 = addl(l1, l2)
l3.travel()
相關文章
相關標籤/搜索