One LeetCode per day (24): Convert an ordered array into a binary search tree

3 minutes a day, embark on the path of algorithm counterattack.

Collection of previous articles

A collection of LeetCode previous articles every day

Code warehouse

GitHub: https://github.com/meteor1993/LeetCode

Gitee : https://gitee.com/inwsy/LeetCode

Topic: Convert an ordered array into a binary search tree

Question source: https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/

Convert an ordered array in ascending order 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.

Example:

Given an ordered array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which can represent the following highly balanced binary search tree:

      0
     / \
   -3   9
   /   /
 -10  5

Problem solving ideas

What the hell? Can a binary tree be reversed through an array? I dare not make BT points for this question.

It's definitely a fairy idea. If you don't play or play, you just surrender and turn over the answer.

In the beginning, one thing needs to be clear, what is a binary search tree?

The salient features of binary search:

  • For each node X in the tree, all key values ​​in its left subtree are less than the key values ​​of X, and all key values ​​in its right subtree are greater than the key values ​​of X.

Simply put, it is left<root<right.

According to this feature, if a binary search tree is traversed in order, then an increasing sequence will be obtained.

Putting it into this question, it is very clear that the array sequence given in this question is actually a binary search tree traversed in order.

In the official solution, there are a few questions that are better, I will extract them (the following content is from the official solution).

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.

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.

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 a difference of 11, 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.

At this point, the official solution has solved the most difficult part "How to determine a root node".

The next step is to put the remaining numbers in the left and right subtrees of the binary tree.

So how to put it? Very simple, we repeat the process just now, such as the left subtree, first find the root node of the left subtree (the number in the middle), and then recurse this process.

I only drew it for two iterations. After the complete drawing, the picture was too big for the PPT to draw.

Three solutions are given on the official problem solution. One is to choose the number on the left of the middle position as the root node, the other is to choose the number on the right of the middle position as the root node, and the last one is to combine the first two.

Problem-solving scheme 1: In-order traversal, select the number on the left of the middle position as the root node

Choose the number on the left of the middle position as the root node, then the subscript of the root node is, where mid = (left + right) / 2the division is integer division.

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 node = new TreeNode(nums[mid]);
    node.left = helper(nums, left, mid - 1);
    node.right = helper(nums, mid + 1, right);
    return node;
}

Problem-solving scheme 2: In-order traversal, select the number on the right of the middle position as the root node

With the above scheme one, it is very simple to write this again. In this scheme, the calculation formula of the root node is mid = (left + right + 1) / 2.

// 中序遍历,选择中间位置右边的数字作为根节点
public TreeNode helper_1(int[] nums, int left, int right) {
    
    
    if (left > right) return null;

    int mid = (left + right + 1) / 2;

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

There are actually only a change midof values, and the rest got nothing changed.

Problem-solving scheme 3: In-order traversal, choose any number in the middle position as the root node

The program midvalue is very interesting, do not look back, you may think at first how to take a middle position arbitrary.

I.e. in mid = (left + right) / 2and mid = (left + right + 1) / 2takes a value between.

I’ll release the official example picture first, don’t rush to scroll down:

The result is a random number can take: mid = (left + right + new Random().nextInt(2)) / 2.

// 中序遍历,选择任意一个中间位置数字作为根节点
public TreeNode helper_2(int[] nums, int left, int right) {
    
    
    if (left > right) return null;

    int mid = (left + right + new Random().nextInt(2)) / 2;

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

The idea of ​​this option three is indeed very strange and interesting.

Today’s question needs to be inversely derived from an array to create a binary search tree. Ahem, I really can’t do it the first time I see it, so I’m going to memorize the answer.

Your scan code attention is the biggest encouragement for the editor to insist on originality:)

Guess you like

Origin blog.csdn.net/meteor_93/article/details/108165005