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

Sequence traversal of < leetcode ladder > day033 binary tree (breadth first search) | 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 !


Recommend a song every day : Maple ——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 a binary tree , Please return to its button Sequence traversal The resulting node value . ( That is, layer by layer , Access all nodes from left to right ).

Example :
Binary tree :[3,9,20,null,null,15,7],

Return its sequence traversal result :

Breadth first search

analysis :

Returns an array of , We also need to initialize the array , But I don't know the size of the array , So it's usually stored in list And then convert it into an array , return list It's simpler .

Borrow the big guy's picture again , That's perfect !

class Solution: def levelOrder(self, root: TreeNode) -> List[List[int]]: if not root: return [] queue = [root] res = [] while queue: n = len(queue) sub_list = [] for i in range(n): node = queue.pop(0) sub_list.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) res.append(sub_list) return res


The result is right , Today is also a tiring day , Come here first , Resting !~

Share today , Yongge ( Maybe it's kind to call it this way ) Finally, let's say our best wishes !~

Reference

author : Power button (LeetCode)
link :https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnarn7/
source : Power button (LeetCode)

author : Data structures and algorithms
link :https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnd69e/?discussion=1Pu6Hw
source : Power button (LeetCode)


Score today :+10
Total score :710

come on. !!!


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