题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5
/*
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode (int x)
{
val = x;
}
}*/
using System;
using System.Collections.Generic;
class Solution
{
TreeNode preNode = null;
public TreeNode Convert(TreeNode pRootOfTree)
{
// write code here
if(pRootOfTree==null) return pRootOfTree;
List<TreeNode> list = new List<TreeNode>();
InOrder(pRootOfTree,list);
for(int i=0;i<list.Count-1;i++)
{
list[i].right = list[i+1];
list[i+1].left= list[i];
}
return list[0];
}
public void InOrder(TreeNode root,List<TreeNode> list)
{
if(root==null)
return;
InOrder(root.left,list);
list.Add(root);
InOrder( root.right,list);
}
}
