forked from apachecn/Interview
-
Notifications
You must be signed in to change notification settings - Fork 10
/
26-二叉搜索树与双向链表.py
executable file
·38 lines (30 loc) · 1018 Bytes
/
26-二叉搜索树与双向链表.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
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def Convert(self, pRootOfTree):
# write code here
if not pRootOfTree:
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
left.right = pRootOfTree
self.Convert(pRootOfTree.right)
right = pRootOfTree.right
if right:
while right.left:
right = right.left
pRootOfTree.right = right
right.left = pRootOfTree
while pRootOfTree.left:
pRootOfTree = pRootOfTree.left
return pRootOfTree