当前位置:   article > 正文

力扣0109——有序链表转换二叉搜索树

力扣0109——有序链表转换二叉搜索树

有序链表转换二叉搜索树

难度:中等

题目描述

给定一个单链表的头节点 head ,其中的元素 按升序排序 ,将其转换为高度平衡的二叉搜索树。

本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差不超过 1。

示例1:

输入: head = [-10,-3,0,5,9]
输出: [0,-3,9,-10,null,5]
解释: 一个可能的答案是[0,-3,9,-10,null,5],它表示所示的高度平衡的二叉搜索树

示例2:

输入: head = []
输出: []

题解:

0108一样的思路,将链表中的元素转存到数组中,之后按照 0108 的思路解题即可

想法代码

using System;
using System.Collections;
using System.Collections.Generic;

public class ListNode
{
    public int val;
    public ListNode next;

    public ListNode(int val = 0, ListNode next = null)
    {
        this.val = val; 
        this.next = next;
    }
}

public class TreeNode
{
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
    {
        this .val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution
{
    public static void Main(string[] args)
    {
        ListNode list = new ListNode
        {
            val = -10,
            next = new ListNode
            {
                val = -3,
                next = new ListNode
                {
                    val = 0,
                    next = new ListNode
                    {
                        val = 5,
                        next = new ListNode
                        {
                            val = 9
                        }
                    }
                }
            }
        };
        Solution solution = new Solution();
        TreeNode ans = solution.SortedListToBST(list);
        Console.ReadKey();
    }

    public TreeNode SortedListToBST(ListNode head)
    {
        Queue<int> queue = new Queue<int>();
        int index = 0;
        while (head != null)
        {
            queue.Enqueue(head.val);
            head = head.next;
        }
        int[] temp = new int[queue.Count];
        while (queue.Count > 0)
        {
            temp[index] = queue.Dequeue();
            index++;
        }

        return BackTrack(temp, 0, temp.Length - 1);
    }

    public TreeNode BackTrack(int[] nums, int start, int end)
    {
        if (start > end)
        {
            return null;
        }
        int mid = (start + end) / 2;
        return new TreeNode(nums[mid], BackTrack(nums, start, mid - 1), BackTrack(nums, mid + 1, end));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/49560
推荐阅读
相关标签
  

闽ICP备14008679号