Ordered arrays and converted into an ordered list AVL tree

Converted into an ordered array of AVL tree

An ordered array in accordance with the ascending order, is converted to a highly balanced binary search tree.

In this problem, a highly balanced binary tree is a binary tree refers to the left and right sub-tree of each node is the height difference between the absolute value of not more than 1.

Example:
Here Insert Picture Description
Legend: dichotomous thinking, merge, depth-first.

/**
 * 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* sortedArrayToBST(vector<int>& nums) {
        return dfs(nums,0,nums.size()-1);
    }
    TreeNode* dfs(vector<int>& nums,int left,int right)
    {
        if(left>right)  return nullptr;
        int mid=(left+right)/2;
        TreeNode* node=new TreeNode(nums[mid]);
        node->left=dfs(nums,left,mid-1);
        node->right=dfs(nums,mid+1,right);
        return node;
    }
};

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

AVL tree is converted into an ordered list

Given a singly-linked list, wherein the elements sorted in ascending order, to convert it to a well-balanced binary search tree.

In this problem, a highly balanced binary tree is a binary tree refers to the left and right sub-tree of each node is the height difference between the absolute value of not more than 1.

Example:
Here Insert Picture Description
Method One: speed indicator intermediate nodes to find
a problem we can access it via index accurately determine the intermediate ordered sequence, list how to find an intermediate node, perhaps you will say the speed pointers. Then we need to go deep traversal recursion, each recursive that need to traverse the pointer speed to find an intermediate node, obtaining an intermediate node O ( N ) O (N) . The overall time complexity is O ( N l O g N ) O (nlogn) , the spatial complexity is O ( l O g N ) O (lie) .

/**
 * 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==nullptr)   return nullptr;
        if(head->next==nullptr)  return new TreeNode(head->val);
        
        ListNode*fast=head,*slow=head,*slow_pre=nullptr;
        while(fast!=nullptr&&fast->next!=nullptr)
        {
            slow_pre=slow;
            slow=slow->next;
            fast=fast->next->next;
        }
        
        TreeNode*node=new TreeNode(slow->val);
        
        if(slow_pre!=nullptr)   slow_pre->next=nullptr;
        
        node->left=sortedListToBST(head);
        node->right=sortedListToBST(slow->next);
        return node;
    }
};

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Method two: space for time
can be stored directly in the array chain, to facilitate access to the intermediate node.

/**
 * 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) {
        vector<int> vec;
        ListNode*cur=head;
        while(cur!=nullptr)
        {
            vec.push_back(cur->val);
            cur=cur->next;
        }
        return dfs(vec,0,vec.size()-1);
    }
    TreeNode* dfs(vector<int>& nums,int left,int right)
    {
        if(left>right)  return nullptr;
        int mid=(left+right)/2;
        TreeNode* node=new TreeNode(nums[mid]);
        node->left=dfs(nums,left,mid-1);
        node->right=dfs(nums,mid+1,right);
        return node;
    }
};

Inorder traversal simulation
preorder binary search tree exactly results in ascending order.

/**
 * 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 {
private:
    ListNode*cur;
public:
    TreeNode* sortedListToBST(ListNode* head) {
        int len=find_list_size(head);
        cur=head;
        return inorder_dfs(0,len-1);
    }
    int find_list_size(ListNode* head)
    {
        int r=0;
        while(head)
        {
            r+=1;
            head=head->next;
        }
        return r;
    }
    TreeNode* inorder_dfs(int left,int right)
    {
        if(left>right)  return nullptr;
        int mid=(left+right)/2;
        
        TreeNode* left_tree=inorder_dfs(left,mid-1);
        TreeNode* node=new TreeNode(cur->val);
        node->left=left_tree;
        cur=cur->next;
        
        node->right=inorder_dfs(mid+1,right);
        return node;
    }
};
Published 139 original articles · won praise 55 · views 60000 +

Guess you like

Origin blog.csdn.net/Vickers_xiaowei/article/details/102532237