-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert-binary-search-tree-to-sorted-doubly-linked-list.py
90 lines (81 loc) · 2.43 KB
/
convert-binary-search-tree-to-sorted-doubly-linked-list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# coding=utf-8
"""
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。
要求不能创建任何新的结点,只能调整树中结点指针的指向。
"""
class TreeNode(object):
def __init__(self, val, left, right):
self.val = val
self.left = None
self.right = None
class Solution(object):
def bst_to_doubly_link(self, root):
"""
这是最先想到的,非递归方法
:param root:
:return:
"""
if not root:
return
stack = []
head = pre = None
while root or stack:
while root:
stack.append(root)
root = root.left
top = stack.pop()
root = top.right
if not head:
head = top
if not pre:
pre = top
else:
pre.right = top
top.left = pre
pre = top
return head
def bst_to_doubly_link1(self, root):
"""
用递归方法试试
:param root:
:return:
"""
if not root:
return
if not root.left and not root.right:
return root
# 对左子树递归,得到左子树转为doubly link的头结点
left = self.bst_to_doubly_link1(root.left)
tail = left
# 从头结点找到尾节点
while tail and tail.right:
tail = tail.right
# 有的根节点没有左子树,这是tail就为空
if tail:
tail.right = root
root.left = tail
right = self.bst_to_doubly_link1(root.right)
if right:
root.right = right
right.left = root
return left if left else root
def bst_to_doubly_link2(self, root):
last_node = None
self._convert_node(root, last_node)
# 找到链表最右边的节点,再从这里遍历到头结点
head = last_node
while head and head.left:
head = head.left
return head
def _convert_node(self, root, last_node):
if not root:
return
current = root
if current.left:
self._convert_node(current.left, last_node)
current.left = last_node
if last_node:
last_node.right = current
last_node = current
if current.right:
self._convert_node(current.right, last_node)