美文网首页
convert-sorted-list-to-binary-se

convert-sorted-list-to-binary-se

作者: DaiMorph | 来源:发表于2019-05-30 00:31 被阅读0次
    class Solution {
    public:
        TreeNode *sortedListToBST(ListNode *head) {
            return create(head,NULL);
        }
        TreeNode*create(ListNode*head,ListNode*tail)
        {
            if(head==tail)return NULL;
            ListNode*slow=head,*fast=head;
            while(fast!=tail&&fast->next!=tail)slow=slow->next,fast=fast->next->next;
            TreeNode*root=new TreeNode(slow->val);
            root->left=create(head,slow);
            root->right=create(slow->next,tail);
            return root;
        }
    };
    

    相关文章

      网友评论

          本文标题:convert-sorted-list-to-binary-se

          本文链接:https://www.haomeiwen.com/subject/xisptctx.html