2
头图

Ordered Linked List Conversion Binary Search Tree

Title description: Given a singly linked list, the elements in it are sorted in ascending order and converted into a highly balanced binary search tree.

In this question, a highly balanced binary tree means that the absolute value of the height difference between the left and right subtrees of each node of a binary tree does not exceed 1.

Please refer to the official website of LeetCode for examples.

Source: LeetCode
Link: https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Solution 1: Recursion

First, convert the linked list into an array, and then the process is exactly the same as the solution of LeetCode-108-Converting an ordered array into a binary search tree

According to the nature of the binary search tree, because the given array is arranged in ascending order, it can be determined that the array num is the middle-order traversal sequence of the binary search tree. In order to obtain a balanced binary tree, take the middle position of the array The node is the root node, so that the nodes of the left and right subtrees are more balanced. The specific processing process is as follows:

  • Call the recursive method, the initial starting position is the length of the array;
  • When the start position is greater than the end position, the node has been traversed, and the empty tree is returned directly;
  • Obtain the value of the middle position as the root node, so that the node tree of the left and right subtrees is more balanced;
  • Then recursively obtain the left and right subtrees of the current root node;
  • Finally, the root node is returned to the balanced binary search tree.
import com.kaesar.leetcode.ListNode;
import com.kaesar.leetcode.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class LeetCode_109 {
    public static TreeNode sortedListToBST(ListNode head) {
        Queue<Integer> queue = new LinkedList<>();
        while (head != null) {
            queue.add(head.val);
            head = head.next;
        }
        int[] nums = new int[queue.size()];
        int index = 0;
        // 先将链表转换成数组,即为二叉树的中序遍历序列
        while (!queue.isEmpty()) {
            nums[index++] = queue.poll();
        }
        // 调用递归方法,初始的起始位置为数组的长度
        return sortedListToBST(nums, 0, nums.length - 1);
    }

    /**
     * 递归
     *
     * @param nums
     * @param left
     * @param right
     * @return
     */
    private static TreeNode sortedListToBST(int[] nums, int left, int right) {
        // 当起点位置大于终点位置时,说明节点已经遍历完了,直接返回空树
        if (left > right) {
            return null;
        }
        // 获取中间位置的值作为根节点,这样左右子树的节点树是较为平衡点
        int mid = (left + right) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        // 然后递归获得当前根节点的左右子树
        root.left = sortedListToBST(nums, left, mid - 1);
        root.right = sortedListToBST(nums, mid + 1, right);
        return root;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(-10);
        head.next = new ListNode(-3);
        head.next.next = new ListNode(-0);
        head.next.next.next = new ListNode(5);
        head.next.next.next.next = new ListNode(9);

        sortedListToBST(head).print();
    }
}
[Daily Message] iron or gold, there will be inkstones that can be worn; every day, every day, every day, there is never a sharp needle.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!