-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path二叉搜索树与双向链表.py
61 lines (51 loc) · 1.5 KB
/
二叉搜索树与双向链表.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
'''
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。
要求不能创建任何新的结点,只能调整树中结点指针的指向。
'''
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def Convert(self, pRootOfTree):
if pRootOfTree == None:
return None
if not pRootOfTree.left and not pRootOfTree.right:
return pRootOfTree
# 处理左子树
self.Convert(pRootOfTree.left)
left = pRootOfTree.left
# 连接根与左子树最大结点
if left:
while left.right:
left = left.right
pRootOfTree.left, left.right = left, pRootOfTree
# 处理右子树
self.Convert(pRootOfTree.right)
right = pRootOfTree.right
# 连接根与右子树最小结点
if right:
while right.left:
right = right.left
pRootOfTree.right, right.left = right, pRootOfTree
while pRootOfTree.left:
pRootOfTree = pRootOfTree.left
return pRootOfTree
pNode1 = TreeNode(8)
pNode2 = TreeNode(6)
pNode3 = TreeNode(10)
pNode4 = TreeNode(5)
pNode5 = TreeNode(7)
pNode6 = TreeNode(9)
pNode7 = TreeNode(11)
pNode1.left = pNode2
pNode1.right = pNode3
pNode2.left = pNode4
pNode2.right = pNode5
pNode3.left = pNode6
pNode3.right = pNode7
S = Solution()
newList = S.Convert(pNode1)
print(newList.val)