赞
踩
题目:
题解:
- class Solution:
- def binaryTreePaths(self, root: TreeNode) -> List[str]:
- paths = list()
- if not root:
- return paths
-
- node_queue = collections.deque([root])
- path_queue = collections.deque([str(root.val)])
-
- while node_queue:
- node = node_queue.popleft()
- path = path_queue.popleft()
-
- if not node.left and not node.right:
- paths.append(path)
- else:
- if node.left:
- node_queue.append(node.left)
- path_queue.append(path + '->' + str(node.left.val))
-
- if node.right:
- node_queue.append(node.right)
- path_queue.append(path + '->' + str(node.right.val))
- return paths
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。