# -*- 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
root=self.dp(pRootOfTree)
while root.left:
root=root.left
return root
def dp(self,root):
if not root:
return None
cur=root
if cur.left:
l=self.dp(root.left)
while l.right:
l=l.right
cur.left=l
l.right=cur
if cur.right:
r=self.dp(root.right)
while r.left:
r=r.left
cur.right=r
r.left=cur
return cur
本文介绍了一种将二叉搜索树转换为双向链表的算法实现,通过深度优先遍历的方式,调整树节点的左右指针,使其形成一个有序的双向链表。此方法不仅保持了原有节点的顺序,还提高了链表操作的效率。
2076

被折叠的 条评论
为什么被折叠?



