题解 | #二叉搜索树与双向链表#

二叉搜索树与双向链表

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);
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务