# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def zhongxu(self,root):
if not root:
return []
return self.zhongxu(root.left)+[root]+self.zhongxu(root.right)
def Convert(self, pRootOfTree):
# write code here
list1=self.zhongxu(pRootOfTree)
if len(list1)==0:
return None
if len(list1)==1:
return pRootOfTree
for i in range(len(list1)-1):
list1[i].right=list1[i+1]
list1[i+1].left=list1[i]
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def zhongxu(self,root):
if not root:
return []
return self.zhongxu(root.left)+[root]+self.zhongxu(root.right)
def Convert(self, pRootOfTree):
# write code here
list1=self.zhongxu(pRootOfTree)
if len(list1)==0:
return None
if len(list1)==1:
return pRootOfTree
for i in range(len(list1)-1):
list1[i].right=list1[i+1]
list1[i+1].left=list1[i]
return list1[0]
使用中序递归遍历存入列表,最后将列表中的节点中指针进行调换,返回list1[0]