Leetcode "Convert Sorted List to Binary Search Tree" Python implementation

Source: Internet
Author: User

title :

Given a singly linked list where elements is sorted in ascending order, convert it to a height balanced BST.

code : OJ Test via runtime:178 ms

1 #Definition for a binary tree node2 #class TreeNode:3 #def __init__ (self, x):4 #self.val = x5 #self.left = None6 #self.right = None7 #8 #Definition for singly-linked list.9 #class ListNode:Ten #def __init__ (self, x): One #self.val = x A #Self.next = None -  - classSolution: the     #@param head, a list node -     #@return a tree node -     defSortedlisttobst (Self, head): -         #Special Case Frist +         ifHead isNone: -             returnNone +         ifHead.next isNone: A             returnTreeNode (head.val) at         #Slow point & fast point trick to divide the list -slow =listnode (0) -Fast =listnode (0) -Slow.next =Head -Fast.next =Head -          whileFast.next is  notNone andFast.next.next is  notNone: inslow =Slow.next -Fast =Fast.next.next toleft =Head +right =Slow.next.next -Root =TreeNode (slow.next.val) theSlow.next.next = None#Cut the connection bewteen right child tree and root TreeNode *Slow.next = None#cut the connection between left child tree and root TreeNode $Root.left =Self.sortedlisttobst (left)Panax NotoginsengRoot.right =Self.sortedlisttobst (right) -         returnRoot

Ideas :

Binary search tree What to figure out first

Because it is an ordered list, it can be used as a recursive way of thinking, from top to bottom.

1. Each time the middle node of the linked list is raised, the part of the middle node of the linked list continues recursively as Zuozi, and the part after the middle node of the linked list continues recursively as the right subtree.

2. The condition for stopping a recursive call is to pass in the past the head is empty (a leaf node is empty) or there is only one listnode (to a leaf node).

When looking for the middle node of the linked list, the technique of using fast and slow pointers: note that because the previous special case has been passed into the empty list and the length of the list of 1 is processed, so the speed of the pointer need to judge from the shortest length of 2 of the list of processing logic. The previous code only judged in the while loop that Fast.next.next was not none and ignored the case with a list length of 2, so it was made up with a fast.next is not none, and the AC was modified once.

There is also a train of thought on the net, only need to walk once the list can complete the conversion, the use is the bottom-up achievement. There is a note in the following log, save to see later.

http://blog.csdn.net/nandawys/article/details/9125233

Leetcode "Convert Sorted List to Binary Search Tree" Python implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.