Merge pull request #1802 from ZerenZhang2022/patch-2

Update 0102.二叉树的层序遍历.md
This commit is contained in:
程序员Carl 2022-12-27 10:23:36 +08:00 committed by GitHub
commit 4de2753df1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 0 deletions

View File

@ -1346,6 +1346,22 @@ class Solution:
return results
```
```python
# LeetCode 429. N-ary Tree Level Order Traversal
# 递归法
class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root: return []
result=[]
def traversal(root,depth):
if len(result)==depth:result.append([])
result[depth].append(root.val)
if root.children:
for i in range(len(root.children)):traversal(root.children[i],depth+1)
traversal(root,0)
return result
```
java:
```java