程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

< leetcode ladder > day034 convert an ordered array into a binary search tree (divide and conquer recursion) | primary algorithm | Python

編輯:Python

Make a little progress every day , It's already great , Insist on , Don't be too tired , Refuse to roll in , Start with practice every day , Ten minutes a day , Live happily all your life ! The epidemic is still repeated , Everyone wear masks ~ Continue to continue , Come on , Today and Brother cheshen Work together to improve your Python Programming and Interview ability Well , brush ladder on high buildings ~

Put on what I took Photo Well ! I bought a hundred courses of tea in the evening , Reward yourself with a wave of , ha-ha

Recommend a song every day : A north ——Jay Chou

The following is my Ladder integral rule

At least one question a day : An integral +10 branch
If you do one more question ( Or one more way to answer ), Then the points of the day +20 branch (+10+10)
If you do more than three , Start with question 3 +20 branch ( Such as : If you do three questions, you will score -10+10+20=40; If you do four questions, you will score –10+10+20+20=60)


Initial classification 100 branch
If you haven't done the problem for one day , Then deduct points -10 branch ( Saturday 、 Except Sunday : rest
insist !!!


Primary algorithm

List of questions

Linked list

stem

Give you an array of integers nums , Where elements have been Ascending array , Please turn it into a Highly balanced Binary search tree .

Highly balanced A binary tree is a tree that satisfies 「 The absolute value of the height difference between the left and right subtrees of each node does not exceed 1 」 Two fork tree .

Example 1:

Input :nums = [-10,-3,0,5,9]
Output :[0,-3,9,-10,null,5]
explain :[0,-10,5,null,-3,null,9] Will also be considered the right answer :


Example 2:

Input :nums = [1,3]
Output :[3,1]
explain :[1,3] and [3,1] They're all highly balanced binary search trees .

Divide and conquer recursion

analysis :

What we need to do is transform it into a highly balanced binary search tree , The title says our array nums It's the one that has finished arranging the serial number , We can use the idea of recursion , The middle value in the array will be taken out every time , Then take the previous number as the left node , The latter number is used as the node of the right subtree , In this way, the allocation of binary tree is completed . We can use slices to recursively take values , Then fill in the sub tree species . Today's question is not very difficult , There is also a deep search to try .

The big man's picture is simply the voice of his heart :

class Solution: def sortedArrayToBST(self, nums: List[int]) -> TreeNode: n = len(nums) if n == 0: return None mid = n // 2 # Plate division , Median  root = TreeNode(nums[mid]) root.left = Solution.sortedArrayToBST(self, nums[:mid]) root.right = Solution.sortedArrayToBST(self, nums[mid+1:]) return root


Binary tree , Come here , Also finished painting , " !

I'm a little free now , Today was too tired , Finally the weekend , Take a rest , incorrect , There has to be a meeting tomorrow , ah !!!

Tomorrow I'll sleep until noon !!!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved