File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -88,6 +88,7 @@ public:
88
88
python代码:
89
89
90
90
```python
91
+ # 迭代法
91
92
class Solution:
92
93
def levelOrder(self, root: TreeNode) -> List[List[int]]:
93
94
if not root:
@@ -108,7 +109,20 @@ class Solution:
108
109
109
110
return out_list
110
111
```
111
-
112
+ ``` python
113
+ # 递归法
114
+ class Solution :
115
+ def levelOrder (self , root : TreeNode) -> List[List[int ]]:
116
+ res = []
117
+ def helper (root , depth ):
118
+ if not root: return []
119
+ if len (res) == depth: res.append([]) # start the current depth
120
+ res[depth].append(root.val) # fulfil the current depth
121
+ if root.left: helper(root.left, depth + 1 ) # process child nodes for the next depth
122
+ if root.right: helper(root.right, depth + 1 )
123
+ helper(root, 0 )
124
+ return res
125
+ ```
112
126
java:
113
127
114
128
``` Java
You can’t perform that action at this time.
0 commit comments