[LeetCode]102. Binary Tree Level Order Traversal
102. Binary Tree Level Order Traversal
class Solution(object):
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
res = []
deque, deque_temp = [root], []
path = []
while deque or deque_temp:
path = []
while deque:
node = deque.pop(0)
path.append(node.val)
if node.left:
deque_temp.append(node.left)
if node.right:
deque_temp.append(node.right)
if path:
res.append(path)
path = []
while deque_temp:
node = deque_temp.pop(0)
path.append(node.val)
if node.left:
deque.append(node.left)
if node.right:
deque.append(node.right)
if path:
res.append(path)
return res
class Solution(object):
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if not root:
return []
res = []
deque, deque_temp = [root], []
while deque:
path = []
while deque:
node = deque.pop(0)
path.append(node.val)
if node.left:
deque_temp.append(node.left)
if node.right:
deque_temp.append(node.right)
if path:
res.append(path)
if deque_temp:
deque = deque_temp
deque_temp = []
return res
![](https://i.loli.net/2018/11/17/5beff4d639cbb.jpg)
关注公众号:数据结构与算法那些事儿,每天一篇数据结构与算法