mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
add python codes of binary tree's level order
This commit is contained in:
@ -80,6 +80,40 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
python代码:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 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
|
||||||
|
class Solution:
|
||||||
|
def levelOrder(self, root: TreeNode) -> List[List[int]]:
|
||||||
|
if not root:
|
||||||
|
return []
|
||||||
|
|
||||||
|
quene = [root]
|
||||||
|
out_list = []
|
||||||
|
|
||||||
|
while quene:
|
||||||
|
in_list = []
|
||||||
|
for i in range(len(quene)):
|
||||||
|
node = quene.pop(0)
|
||||||
|
in_list.append(node.val)
|
||||||
|
if node.left:
|
||||||
|
quene.append(node.left)
|
||||||
|
if node.right:
|
||||||
|
quene.append(node.right)
|
||||||
|
|
||||||
|
out_list.append(in_list)
|
||||||
|
|
||||||
|
return out_list
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
javascript代码:
|
javascript代码:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
Reference in New Issue
Block a user