(74)109. Ordered linked list conversion binary search tree (leetcode).

题目链接:
https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/
难度:中等
109. 有序链表转换二叉搜索树
	给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。
	本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。
示例:
	给定的有序链表: [-10, -3, 0, 5, 9],
	一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树:
	      0
	     / \
	   -3   9
	   /   /
	 -10  5

will not. . . It’s not that the problem is difficult to pay attention to, but the knowledge of the tree is too little. Some are unfamiliar. You can only ask for help. The answer is the same as when you have done a math problem. A
highly balanced binary search tree: Take the midpoint of the linked list as the root. To balance the tree, the left subtree with the point before the midpoint as the root node repeats the right subtree with the point after the midpoint as the root node,
left closed, right open

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    
    
public:
    // 快慢指针  第一次听说。。。
    // 就是每次循环中  fast+2 slow+1 这样slow就是中点
    ListNode* getMedian(ListNode* left, ListNode* right) {
    
    
        ListNode* fast = left;
        ListNode* slow = left;
        while (fast != right && fast->next != right) {
    
    
            fast = fast->next->next;
            // fast = fast->next;
            slow = slow->next;
        }
        return slow;
    }
	// 左闭右开  不包括 right
    TreeNode* buildTree(ListNode* left,ListNode* right){
    
    
        if(left==right){
    
    
            return nullptr;
        }
        // 取中点 作为根节点
        ListNode* mid=getMedian(left,right);
        TreeNode* root=new TreeNode(mid->val);
        // 递归  根节点的左右子树
        root->left=buildTree(left,mid);
        root->right=buildTree(mid->next,right);
        return root;
    }

    TreeNode* sortedListToBST(ListNode* head) {
    
    
        return buildTree(head,nullptr);
    }
};

The in-order traversal combined with the in-order traversal of the binary search tree is the linked list (both sides are closed)

class Solution {
    
    
public:
	// 得到链表的长度
    int getLength(ListNode* r){
    
    
        int i=0;
        while(r!=nullptr){
    
    
            ++i;
            r=r->next;
        }
        return i;
    }
	// 中序遍历 左-根-右  注意:head前有 &
    TreeNode* buildTree(ListNode* &head,int left,int right){
    
    
        if(left>right){
    
    
            return nullptr;
        }
        int mid=(left+right+1)/2;
        TreeNode* root=new TreeNode();
        // 左 
        root->left=buildTree(head,left,mid-1);
        // 根  递归之后head所指向的就是mid
        // 右是从 mid+1 开始的
        root->val=head->val;
        head=head->next;
        // 右
        root->right=buildTree(head,mid+1,right);
        return root;
    }

    TreeNode* sortedListToBST(ListNode* head) {
    
    
        int length=getLength(head);
        return buildTree(head,0,length-1);
    }
};

Guess you like

Origin blog.csdn.net/li_qw_er/article/details/108074509