当前位置:网站首页>【LeetCode】102. Sequence traversal of binary tree
【LeetCode】102. Sequence traversal of binary tree
2022-06-12 22:21:00 【LawsonAbs】
1 subject
Hierarchical output of binary tree .
2 thought
A common way to solve this problem is to use bfs Write , But sometimes the interviewer asks you to do something dfs edition , So here we use dfs Achieve it . But it also needs the help of the amount of space .
3 Code
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from collections import defaultdict
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if root is None:
return []
res = defaultdict(list)
self.dfs(root,0,res) # The initial height is 0
return [ val for key,val in res.items()]
# height Indicates the height of the current node
def dfs(self,root,height,res):
# Put in value
res[height].append(root.val)
if root.left is not None:
self.dfs(root.left,height+1,res)
if root.right is not None:
self.dfs(root.right,height+1,res)
边栏推荐
- flutter系列之:flutter中常用的GridView layout详解
- 项目里面的traceID的设计
- QT quick 3D learning: use mouse and keyboard to control node position and direction
- List of open source alternative projects of world famous Cloud Service SaaS companies
- LNMP platform docking redis service
- Mysql concat_ws、concat函数使用
- Preliminary use of jvisualvm
- 最近公共祖先问题你真的学会了吗?
- C # reading table data in word
- Step by step evolution of restful API version Frankel
猜你喜欢

JVM foundation - > three ⾊ mark

Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?

C语言:如何给全局变量起一个别名?

You can move forward or backward. This function in idea is amazing!
![[data analysis] data clustering and grouping based on kmeans, including Matlab source code](/img/76/deec6cf60c0d02e99ebc3e21d3b8a4.png)
[data analysis] data clustering and grouping based on kmeans, including Matlab source code

微信小程序提现功能

PE installation win10 system

How to perform disaster recovery and recovery for kubernetes cluster? (22)

About the solution to "the application cannot start normally 0xc00000022" after qt5.15.2 is installed and qtcreator is started

Redis optimization
随机推荐
【Web技术】1348- 聊聊水印实现的几种方式
China's alternative sports equipment market trend report, technology dynamic innovation and market forecast
JVM Basics - > how to troubleshoot JVM problems in your project
PCB package download website recommendation and detailed usage
[Jianzhi offer] Jianzhi offer 05 Replace spaces
Design a MySQL table for message queue to store message data
JVM foundation > GC generation: minorgc majorgc fullgc mixed GC
[image denoising] image denoising based on trilateral filter with matlab code
Create a virtual thread using loom - David
Mr. Sun's version of JDBC (21:34:25, June 12, 2022)
Can tonghuashun open an account? Is it safe to open an account in tonghuashun? How to open a securities account
flutter系列之:flutter中常用的GridView layout详解
Redis optimization
接口测试工具apipost3.0版本对于流程测试和引用参数变量
(downloadable) Research Report on the development and utilization of government data (2021), a glimpse of the development of Government Office
Generate the chrysanthemum code of the applet (generate the chrysanthemum code, change the middle logo, change the image size, and add text)
QT quick 3D learning: mouse picking up objects
Hostvars in ansible
Is it safe to open an account in flush? How to open an account online to buy stocks
JVM foundation - > talk about class loader two parent delegation model