June 30, 2022 leetcode daily check-in - 108. Convert an ordered array to a binary search tree

1. Topic description and requirements

108. Convert Sorted Array to Binary Search Tree - LeetCode

topic description

Given an integer array nums, the elements of which have been arranged in ascending order , please convert it into a height-balanced binary search tree.

A height-balanced binary tree is a binary tree that satisfies "the absolute value of the height difference between the left and right subtrees of each node does not exceed 1" .

example

Example 1:

输入:nums = [-10,-3,0,5,9]
输出:[0,-3,9,-10,null,5]
解释:[0,-10,5,null,-3,null,9] 也将被视为正确答案:

 

Example 2:

 

输入:nums = [1,3]
输出:[3,1]
解释:[1,null,3] 和 [3,1] 都是高度平衡二叉搜索树。

hint

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • nums in  strict ascending  order

2. Problem-solving ideas

General idea:

It should be converted into a height-balanced binary search tree according to the given integer array, that is, the absolute value of the height difference between the left and right subtrees does not exceed 1. Then the number of nodes in the left and right subtrees needs to be equal or the difference is 1, so as to balance the height of the binary tree. For this, we can choose the method of binary search, set the left pointer left to point to the first element of the array, the right pointer right to point to the last element of the array, and the middle pointer mid to point to the middle element of the array, if there are two middle elements then Choose the one on the left. Use this idea to divide the array into two more average parts, then use the middle element as the root node, apply for the node space for the root node, and then assign it. The next step is to build the left and right subtrees, use the first half of the array as the node of the left subtree, use the above method to continue to build the left subtree, use the second half of the array as the node of the right subtree, use the above method to build the right subtree Tree. Finally, the binary tree is returned after the establishment is completed.
Note: There may be various results. Here, hierarchical traversal is used to traverse the established binary tree. However, in the process of building a binary tree, different elements selected as the root node will create different binary trees. If you select the element on the right side of the middle position The binary tree built for the nodes will also have a certain difference.

Specific steps:

1. Determine whether the given array is empty. If it is empty, the binary tree to be built is also an empty tree. 2. The
pointer mid in the definition, if the left side of the middle position is selected as the root node, then mid=(left+right)/2, if selected The right side of the middle position is the root node, then mid=(left+right+1)/2.
3. Assign a value to the root node and recursively call the function to build the left subtree and right subtree. At the same time, pay attention to the range of the interval in the function parameter. The left subtree should be (left, mid-1), and the right subtree should be (mid +1, right).
4. The establishment is successful, and the traversal result of the binary tree is returned in the form of hierarchical traversal.

3. Specific code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

struct TreeNode* build(int* nums,int left,int right)
{
    if(left>right)//如果给定的数组为空树则返回空
      return NULL;
    int mid=(left+right)/2;//选择给定数组的中间位置作为根结点(如果元素个数为偶数则选择中间位置左边的数字作为根结点)
    struct TreeNode* root=(struct TreeNode*)malloc(sizeof(struct TreeNode));//申请结点空间
    root->val=nums[mid];//为根结点赋值
    root->left=build(nums,left,mid-1);//选定根节点之后根结点左边部分为左子树,继续调用build函数建立左子树
    root->right=build(nums,mid+1,right);//缩小范围,根结点的右边元素构成右子树,调用build函数建立右子树
    return root;//建立完成后返回建立成功的二叉树
}

struct TreeNode* sortedArrayToBST(int* nums, int numsSize){
    return build(nums,0,numsSize-1);
}

Guess you like

Origin blog.csdn.net/m0_59800431/article/details/125532760