109、有序链表转换二叉搜索树

  1. 109、有序链表转换二叉搜索树
    1. 示例:
  2. 题解
    1. 1、递归——二分建树
    2. 2、现将链表转换成数组,再递归(和108解法 1 一样)
    3. 3、中序遍历模拟——巧妙呀

109、有序链表转换二叉搜索树

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

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

示例:

给定的有序链表: [-10, -3, 0, 5, 9],

一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树:

      0
     / \
   -3   9
   /   /
 -10  5

链接:https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree

题解

1、递归——二分建树

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    // 找到链表的中间节点
    private ListNode findMiddleElement(ListNode head) {
        ListNode prev = null;
        ListNode slow = head;
        ListNode fast = head;

        while (fast != null && fast.next != null) {
            prev = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        // 将链表断开
        if (prev != null) {
            prev.next = null;
        }
        // 返回中间节点
        return slow;
    }

    public TreeNode sortedListToBST(ListNode head) {
        if (head == null) {
            return null;
        }

        ListNode mid = findMiddleElement(head);

        TreeNode node = new TreeNode(mid.val);
        // 只有一个节点
        if (head == mid) {
            return node;
        }

        node.left = sortedListToBST(head);
        node.right = sortedListToBST(mid.next);
        // 返回根节点
        return node;
    }
}

2、现将链表转换成数组,再递归(和108解法 1 一样)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    private List<Integer> values;

    public Solution() {
        values = new ArrayList<Integer>;
    }

    private void mapListToValues(ListNode head) {
        while (head != null) {
            values.add(head.val);
            head = head.next;
        }
    }

    private TreeNode convertListToBST(int left,int right) {
        if (left > right) {
            return null;
        }

        int mid = (left + right) / 2;
        TreeNode node = new TreeNode(values.get(mid));

        if (left == right) {
            return node;
        }

        node.left = convertListToBST(left, mid - 1);
        node.right = convertListToBST(mid + 1, right);
        return node;
    }

    public TreeNode sortedListtoBST(ListNode head) {
        mapListToValues(head);

        return convertListToBST(0,values.size() - 1);
    }
}

3、中序遍历模拟——巧妙呀

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    private ListNode head;

    private int findSize(ListNode head){
        ListNode ptr = head;
        int c = 0;
        while (ptr != null) {
            c++;
            ptr = ptr.next;
        }
        return c;
    }

    // 二叉树的中序递归,通过二分,下标 模拟,递归的顺序就是有序链表的顺序
    private TreeNode convertListToBST(int l,int r) {
        if (l > r) {
            return null;
        }

        int mid = (l + r) / 2;

        TreeNode left = convertListToBST(l,mid - 1);

        TreeNode node = new TreeNode (head.val);
        node.left = left;

        head = head.next;

        node.right = convertListToBST(mid + 1, r);

        return node;
    }

    public TreeNode sortedListToBST(ListNode head) {
        int size = findSize(head);

        this.head = head;

        return convertListToBST(0 ,size - 1);
    }

}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1056615746@qq.com

💰

Title:109、有序链表转换二叉搜索树

Count:679

Author:攀登

Created At:2020-07-26, 00:19:44

Updated At:2024-06-15, 15:52:32

Url:http://jiafeimao-gjf.github.io/2020/07/26/109%E3%80%81%E6%9C%89%E5%BA%8F%E9%93%BE%E8%A1%A8%E8%BD%AC%E6%8D%A2%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

×

Help us with donation