LeetCode|LeetCode 102. Binary Tree Level Order Traversal
解题思路
在while循环中遍历每一层(curr_node_list)
将curr_node_list中每一个元素的val存入该层的值的list(temp_val_list)
将curr_node_list中每一个元素的left和right依次存入该层的子结点的list(temp_son_list)
层遍历结束后,更新curr_node_list
while退出条件:curr_node_list为空
【LeetCode|LeetCode 102. Binary Tree Level Order Traversal】原题链接
欢迎在我的博客轻松探索更多思路
代码
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
result=[]
curr_node_list=[]
curr_node_list.append(root)while(curr_node_list):
temp_son_list=[]
temp_val_list=[]
for father in curr_node_list:
if father:
temp_val_list.append(father.val)
try:
temp_son_list.append(father.left)
except:
pass
try:
temp_son_list.append(father.right)
except:
pass
if(temp_val_list):
result.append(temp_val_list)
curr_node_list=temp_son_list
return result
推荐阅读
- LeetCode编程题解法汇总|力扣解法汇总599-两个列表的最小索引总和
- [Golang]力扣Leetcode—剑指Offer—数组—53 - II. 0~n-1中缺失的数字(求和、二分法)
- 每日leetcode——42. 接雨水
- [Golang]力扣Leetcode—剑指Offer—数组—53|[Golang]力扣Leetcode—剑指Offer—数组—53 - I. 在排序数组中查找数字 I(哈希表、遍历)
- Leetcode3无重复字符的最长子串(滑动窗口解法)
- Leetcode|leetcode-蜡烛之间的盘子(经典空换时)
- 每日leetcode——739. 每日温度
- Leetcode120三角形最小路径和
- LeetCode编程题解法汇总|力扣解法汇总258-各位相加
- LeetCode刷题笔记(数组中重复的数据)