I have a problem of "None does not have .val" in my solution... I wonder how to debug it...
Here is the description
- Convert Binary Search Tree to Sorted Doubly Linked List
Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list.]
Let's take the following BST as an example, it may help you understand the problem better:We want to transform this BST into a circular doubly linked list. Each node in a doubly linked list has a predecessor and successor. For a circular doubly linked list, the predecessor of the first element is the last element, and the successor of the last element is the first element.
The figure below shows the circular doubly linked list for the BST above. The "head" symbol means the node it points to is the smallest element of the linked list.
https://www.lintcode.com/problem/convert-binary-search-tree-to-sorted-doubly-linked-list/description
Explanation: Left: reverse output Right: positive sequence output
My code:
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)