Skip to content

Latest commit

 

History

History
102 lines (82 loc) · 2.49 KB

109-convert-sorted-list-to-binary-search-tree.md

File metadata and controls

102 lines (82 loc) · 2.49 KB

109. Convert Sorted List to Binary Search Tree - 有序链表转换二叉搜索树

给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。

本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。

示例:

给定的有序链表: [-10, -3, 0, 5, 9],

一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树:

      0
     / \
   -3   9
   /   /
 -10  5

题目标签:Depth-first Search / Linked List

题目链接:LeetCode / LeetCode中国

题解

Language Runtime Memory
cpp 20 ms 2.6 MB
/**
 * 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) {}
 * };
 */
static auto _ = [](){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return 0;
}();

class Solution {
public:
    void build(TreeNode*& ret, int st, int ed) {
        if (st > ed) return;
        if (st == ed) {
            ret = new TreeNode(0);
            return;
        }
        int mid = (st + ed) / 2;
        ret = new TreeNode(0);
        build(ret->left, st, mid-1);
        build(ret->right, mid+1, ed);
    }

    void inorder(TreeNode*& ret, ListNode*& cur) {
        if (!cur) return;
        if (ret->left) {
            inorder(ret->left, cur);
        }
        ret->val = cur->val;
        cur = cur->next;
        if (ret->right) {
            inorder(ret->right, cur);
        }
    }

    TreeNode* sortedListToBST(ListNode* head) {
        if (!head) return nullptr;
        int n = 0;
        ListNode* p = head;
        while (p) {
            n++;
            p = p->next;
        }
        TreeNode* ret = nullptr;
        build(ret, 0, n-1);  // build a tree and assign initial values
        inorder(ret, head);  // inorder traversal and assign values
        return ret;
    }
};