Skip to content

Commit a768ae1

Browse files
committed
将二叉查找树转换成双链表
1 parent 3d142b3 commit a768ae1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
def __init__(self):
5+
self.head, self.tail = None, None
6+
7+
"""
8+
@param root, the root of tree
9+
@return: a doubly list node
10+
"""
11+
def bstToDoublyList(self, root):
12+
# Write your code here
13+
# 还是中序遍历,然后插入双链表节点。
14+
if root:
15+
self.bstToDoublyList(root.left)
16+
node = DoublyListNode(root.val)
17+
if not self.head:
18+
self.head = node
19+
else:
20+
self.tail.next = node
21+
node.prev = self.tail
22+
self.tail = node
23+
self.bstToDoublyList(root.right)
24+
return self.head

0 commit comments

Comments
 (0)