mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Update 0102.二叉树的层序遍历.md
# LeetCode 429. N-ary Tree Level Order Traversal python, 递归法
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user