[LeetCode-Java Exercise] 108. Convert an ordered array to a binary search tree (simple)

1. Title description

Insert picture description here

2. Problem solving ideas

The middle-order traversal of the binary search tree is an ascending sequence, and the array given by the title is an ordered array sorted in ascending order, so it can be ensured that the array is the middle-order traversal sequence of the binary search tree.

Given the in-order traversal of the binary search tree, can the binary search tree be uniquely determined? the answer is negative. If the height balance of the binary search tree is not required, any number can be used as the root node of the binary search tree, so there are multiple possible binary search trees.
Insert picture description here
If a restriction condition is added, that is, the height balance of the binary search tree is required, can the binary search tree be uniquely determined? The answer is still no.
Insert picture description here
Intuitively, we can choose the middle number as the root node of the binary search tree, so that the number of numbers assigned to the left and right subtrees is the same or only differs by 1, which can keep the tree balanced. If the length of the array is odd, the choice of the root node is unique. If the length of the array is even, you can choose the number on the left of the middle position as the root node or the number on the right of the middle position as the root node, and choose a different number as the root node The created balanced binary search tree is also different.
Insert picture description here
After determining the root node of the balanced binary search tree, the rest of the numbers are located in the left and right subtrees of the balanced binary search tree. The left and right subtrees are also balanced binary search trees, so they can be recursively Way to create a balanced binary search tree.
The base case of recursion is that the balanced binary search tree does not contain any numbers, and the balanced binary search tree is empty at this time.

In the case of a given in-order traversal sequence array, the numbers in each subtree must be continuous in the array, so the numbers contained in the subtree can be determined by the array subscript range, the subscript range is denoted by [left,right ]. For the entire in-order traversal sequence, the subscript ranges from left=0 to right=nums.length−1. When left>right, the balanced binary search tree is empty.

Method: In-order traversal, always choose the number on the left of the middle position as the root node and
choose the number on the left of the middle position as the root node, then the subscript of the root node is mid=(left+right)/2, where the division is an integer division.
Insert picture description here

3. Code implementation

class Solution {
    
    
    public TreeNode sortedArrayToBST(int[] nums) {
    
    
        return helper(nums, 0, nums.length - 1);
    }

    public TreeNode helper(int[] nums, int left, int right) {
    
    
        if (left > right) {
    
    
            return null;
        }

        // 总是选择中间位置左边的数字作为根节点
        int mid = (left + right) / 2;

        TreeNode root = new TreeNode(nums[mid]);
        root.left = helper(nums, left, mid - 1);
        root.right = helper(nums, mid + 1, right);
        return root;
    }
}

Guess you like

Origin blog.csdn.net/weixin_48683410/article/details/114241119