[leetcode]Convert Sorted List to Binary Search Tree

jun·2021년 4월 9일
0
post-thumbnail

유의할점

Linked list 에서 중간 구하기.

풀이

Linked list에서 중간을 구한다. (fast , slow를 이용한다)

중간을 구해서 왼쪽서브트리 / 오른쪽서브트리를 만든다.

코드

C++

/**
 * 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:
    TreeNode* toBST(ListNode* head, ListNode* tail){
        if(head==tail)
            return NULL;
        
        ListNode* fast = head;
        ListNode* slow = head;
        
        while(fast!=tail&&fast->next!=tail){
            fast = fast->next->next;
            slow = slow->next;
        }
        
        TreeNode* root = new TreeNode(slow->val);
        root->left = toBST(head,slow);
        root->right = toBST(slow->next,tail);
        return root;
    }
    
    TreeNode* sortedListToBST(ListNode* head) {
        return toBST(head,NULL);
    }
};
profile
Computer Science / Algorithm / Project / TIL

0개의 댓글