二叉搜索树与双向链表

输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。

 1 # -*- coding:utf-8 -*-
 2 # class TreeNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7 class Solution:
 8     def __init__(self):
 9         self.l = []
10     def inOrder(self,root):
11         if root != None:
12             self.inOrder(root.left)
13             self.l.append(root)
14             self.inOrder(root.right)
15             
16     def Convert(self, pRootOfTree):
17         self.inOrder(pRootOfTree)
18         if pRootOfTree == None:
19             return None
20         for i in range(1,len(self.l)):
21             self.l[i-1].right = self.l[i]
22             self.l[i].left = self.l[i-1]
23         if len(self.l) > 0:
24             self.l[0].left = None
25             self.l[-1].right = None
26         return self.l[0]
27         # write code here

 

posted on 2019-06-14 03:06  Sempron2800+  阅读(236)  评论(0编辑  收藏  举报