【问题标题】:Leetcode:convert-binary-search-tree-to-sorted-doubly-linked-listLeetcode:将二进制搜索树转换为排序的双向链表
【发布时间】:2020-11-19 16:02:43
【问题描述】:

我的解决方案中出现“没有.val”的问题... 我想知道如何调试它...

这里是描述

  1. 将二叉搜索树转换为有序双向链表

将 BST 原地转换为有序循环双向链表。将左右指针视为双向链表中前一个和下一个指针的同义词。]

我们以下面的 BST 为例,它可以帮助你更好地理解问题:我们想把这个 BST 转换成一个循环双向链表。双向链表中的每个节点都有一个前任和后继。对于循环双向链表,第一个元素的前身是最后一个元素,最后一个元素的后继是第一个元素。

下图显示了上面 BST 的循环双向链表。 “头”符号表示它指向的节点是链表的最小元素。

https://www.lintcode.com/problem/convert-binary-search-tree-to-sorted-doubly-linked-list/description

说明: 左:反向输出 右:正序输出

我的代码:

class Solution:
    """
    @param root: root of a tree
    @return: head node of a doubly linked list
    """
    def treeToDoublyList(self, root):
        # Write your code here.
        if not root: 
            return None
        
        prev = node(0)
        self.treeToDoublyList(root.left)
        
        if prev is None:
            
            prev = root
        else:
            prev.left = root
            root.right = prev
        
        
        
        
        self.treeToDoublyList(root.right)

【问题讨论】:

    标签: python linked-list tree


    【解决方案1】:

    给定节点类:

    class Node:
        def __init__(self, val, left=None, right=None):
            self.val = val
            self.left = left
            self.right = right
    

    ...您可以先定义一个按顺序生成节点的函数:

    class Solution:
        def inorder(self, tree):
            if tree.left:
                yield from self.inorder(tree.left)
            yield tree
            if tree.right:
                yield from self.inorder(tree.right)   
    

    那么main函数的实现就变成小菜一碟了:

        def treeToDoublyList(self, root):
            if not root:
                return
            prev = None
            for curr in self.inorder(root):
                # Create double link between prev and curr
                if prev:
                    prev.right = curr
                else:
                    head = curr
                curr.left = prev
                # Prepare for next iteration
                prev = curr
            # Close the cycle
            curr.right = first
            head.left = curr
            return head
    

    为了测试双向链表,你可以再定义两个函数:

        def forward(self, head):
            node = head
            yield node.val
            while node.right != head:
                node = node.right
                yield node.val
    
        def backward(self, head):
            node = head
            yield node.val
            while node.left != head:
                node = node.left
                yield node.val
    

    调用这些函数如下:

    sol = Solution()
    # Example tree
    tree =  Node(4, 
                Node(2, 
                    Node(1), Node(3)
                ),
                Node(5)
            )
    res = sol.treeToDoublyList(tree)
    print(list(sol.forward(res)))  # [1, 2, 3, 4, 5]
    print(list(sol.backward(res)))  # [1, 5, 4, 3, 2]
    

    【讨论】:

    • 对这个答案没有反应?
    猜你喜欢
    • 1970-01-01
    • 2012-07-15
    • 2020-12-28
    • 2018-11-05
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多