LeetCode //C - 109. Convert Sorted List to Binary Search Tree

109. Convert Sorted List to Binary Search Tree

Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced binary search tree.
 

Example 1:

在这里插入图片描述

Input: head = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.

Example 2:

Input: head = []
Output: []

Constraints:
  • The number of nodes in head is in the range [ 0 , 2 ∗ 1 0 4 ] [0, 2 * 10^4] [0,2104].
  • − 1 0 5 < = N o d e . v a l < = 1 0 5 -10^5 <= Node.val <= 10^5 105<=Node.val<=105

From: LeetCode
Link: 109. Convert Sorted List to Binary Search Tree


Solution:

Ideas:
  1. Find the Middle Element: The middle element of the sorted linked list will be the root of the BST. The left half of the list will form the left subtree, and the right half will form the right subtree.
  2. Recursive Approach: Use recursion to build the tree. For each recursive call, find the middle of the current segment of the linked list, create a tree node, and recursively build the left and right subtrees.
Code:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* createTreeNode(int val) {
    struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    newNode->val = val;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

// Helper function to find the middle element of the linked list.
struct ListNode* findMiddleElement(struct ListNode* head) {
    struct ListNode* prevPtr = NULL;
    struct ListNode* slowPtr = head;
    struct ListNode* fastPtr = head;
    
    // Move fastPtr by 2 and slowPtr by 1
    while (fastPtr != NULL && fastPtr->next != NULL) {
        prevPtr = slowPtr;
        slowPtr = slowPtr->next;
        fastPtr = fastPtr->next->next;
    }
    
    // Handling the case when slowPtr might be the head
    if (prevPtr != NULL) {
        prevPtr->next = NULL;
    }
    
    return slowPtr;
}

struct TreeNode* sortedListToBST(struct ListNode* head) {
    // Base case
    if (head == NULL) {
        return NULL;
    }
    
    // Find the middle element of the linked list
    struct ListNode* mid = findMiddleElement(head);
    
    // The middle element becomes the root
    struct TreeNode* node = createTreeNode(mid->val);
    
    // Base case when there is just one element in the linked list
    if (head == mid) {
        return node;
    }
    
    // Recursively construct the left subtree and right subtree
    node->left = sortedListToBST(head);
    node->right = sortedListToBST(mid->next);
    
    return node;
}

// Function to print the tree in order for testing
void inorderTraversal(struct TreeNode* root) {
    if (root == NULL) {
        return;
    }
    inorderTraversal(root->left);
    printf("%d ", root->val);
    inorderTraversal(root->right);
}

相关推荐

  1. LeetCode hot100-19

    2024-05-25 22:12:32       40 阅读
  2. 【力扣100189.轮转数组

    2024-05-25 22:12:32       59 阅读
  3. 面试经典150题(101-104)

    2024-05-25 22:12:32       46 阅读
  4. PYTHON 120道题目详解(100-102

    2024-05-25 22:12:32       47 阅读
  5. 安卓kotlin面试题 101-105

    2024-05-25 22:12:32       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-25 22:12:32       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-25 22:12:32       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-25 22:12:32       87 阅读
  4. Python语言-面向对象

    2024-05-25 22:12:32       96 阅读

热门阅读

  1. Flutter 中的 AnimatedList 小部件:全面指南

    2024-05-25 22:12:32       41 阅读
  2. MySQL InnoDB 引擎的多版本并发控制MVCC

    2024-05-25 22:12:32       32 阅读
  3. Log4j

    2024-05-25 22:12:32       27 阅读
  4. 【数据结构与算法 | 基础篇】环形数组模拟队列

    2024-05-25 22:12:32       38 阅读
  5. 如何抓到微软模拟飞行.dmp文件

    2024-05-25 22:12:32       27 阅读
  6. 在Ubuntu20.04.6上编译Qt5.15.2源码并安装

    2024-05-25 22:12:32       32 阅读
  7. ROS+UBUNTU开发常用指令

    2024-05-25 22:12:32       31 阅读
  8. Ubuntu 安装libpng12的方法

    2024-05-25 22:12:32       31 阅读