[Daily Question] 136. Ordered Chain List Conversion Binary Search Tree (BST Tree, Recursive, Multi-method)

Posted by ginginca on Wed, 12 Feb 2020 05:52:52 +0100

Article Directory

1. Title Source

Links: Converts an ordered array to a binary search tree
Source: LeetCode

2. Title Description

Given a binary tree, determine whether it is a highly balanced binary tree.

In this topic, a height-balanced binary tree is defined as:

  • The absolute difference between the left and right subtrees of each node of a binary tree is no more than 1.

Explain:

You can assume that k is always valid and that 1 < k < the number of binary search tree elements.

Example 1:

Given a binary tree [3,9,20,null,null,15,7]

    3
   / \
  9  20
    /  \
   15   7
 Returns true.

Example 2:

Given Binary Tree [1,2,2,3,3,null,null,4,4]

       1
      / \
     2   2
    / \
   3   3
  / \
 4   4
 Returns false.

3. Topic Analysis

Method 1: Fast and slow pointer method, internal recursion method

This question requires that an ordered list of chains be converted to a binary search tree, which is exactly the same as the previous idea, except that the data types of operations are different, one is an array, the other is a list of chains.Array convenience makes it easy to access any element directly through an index, but chain lists don't.

Because the dichotomy method needs to find the midpoint each time, and

  • The middle point of a chain table can be found by fast or slow pointer operation.
  • Build a root node of the tree with the value of the midpoint.
  • Break the original list into two chains, one before the other, and the other after it. Neither can contain the original middle node. Then recursively call the original function on the two chains, and connect the left and right sub-nodes respectively.

See the code below:

// Execution time: 20 ms, beat 97.62% of all C++ submissions
// Memory consumption: 24.6 MB, beating 37.78% of all C++ submissions

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode *sortedListToBST(ListNode* head) {
        if (!head) 
            return NULL;
        if (!head->next) 
            return new TreeNode(head->val);
        ListNode *slow = head, *fast = head, *last = slow;
        while (fast->next && fast->next->next) {
            last = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        fast = slow->next;
        last->next = NULL;
        TreeNode *cur = new TreeNode(slow->val);
        if (head != slow) 
            cur->left = sortedListToBST(head);
        cur->right = sortedListToBST(fast);
        return cur;
    }
};
Method 2: External recursion

The following recursive methods can also be used:

  • Rewrite a recursive function with two input parameters starting and ending points in the subchain table
  • Knowing these two points, the range of the list can be determined

Convert the middle part directly to a binary search tree, and the content of the recursive function is very similar to that of the above solution.

See the code below:

// Execution time: 32 ms, defeated 69.24% of all C++ submissions
// Memory consumption: 24.9 MB, beating 22.57% of all C++ submissions

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* sortedListToBST(ListNode* head) {
        if (!head) 
            return NULL;
        return helper(head, NULL);
    }
    TreeNode* helper(ListNode* head, ListNode* tail) {
        if (head == tail) 
            return NULL;
        ListNode *slow = head, *fast = head;
        while (fast != tail && fast->next != tail) {
            slow = slow->next;
            fast = fast->next->next;
        }
        TreeNode *cur = new TreeNode(slow->val);
        cur->left = helper(head, slow);
        cur->right = helper(slow->next, tail);
        return cur;
    }
};
241 original articles were published. 43% were praised. 30,000 visits+
Private letter follow