problem
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
solution
返回分为两层,最里面为路径str,外面是路径的list
错误解答
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param {TreeNode} root
# @return {string[]}
def binaryTreePaths(self, root,string='',strings=[]):
if root is None:
strings.append(string[:-2])
return strings
else:
string += str(root.val) + '->'
return self.binaryTreePaths(root.left,string,strings) + self.binaryTreePaths(root.right,string,strings)
Your input
[1,2,3,null,5]
Your answer
["1->2","1->2->5","1->2->5","1->2","1->2->5","1->2->5","1->2","1->2->5","1->2->5","1->2","1->2->5","1->2->5","1->3","1->3","1->2","1->2->5","1->2->5","1->3","1->3"]
Expected answer
["1->2->5","1->3"]
- 从结果分析有两个问题
1.部分题意理解的不对,例如原题中2,left是空的,这时候不需要返回1->2
- 最里层str重复出现,需要检查逻辑,或者变set去重 (#set去重不行,可能会有同名路径;改逻辑的话,就是要更改调用list.append时间点;或者用直接的方法,使用全局变量,这样迭代里面返回的左右不用加了,直接返回self.strings
- )
- 注意点
list 浅copy问题,差点跳进去
修正问题1的版本
def binaryTreePaths(self, root, string=''):
left,right= [],[]
string += str(root.val) + '->'
print 'str++:',string
if root.left is None and root.right is None:
self.strings.append(string[:-2])
return self.strings
if root.left:
left = self.binaryTreePaths(root.left, string)
if root.right:
right = self.binaryTreePaths(root.right, string)
return left+right
修正问题2的版本
- 对于‘->’的处理有点粗糙,影响一点效率
class Solution:
# @param {TreeNode} root
# @return {string[]}
def __init__(self):
self.strings = []
def binaryTreePaths(self, root, string=''):
#特殊校验 None
if root:
string += str(root.val) + '->'
if root.left is None and root.right is None:
self.strings.append(string[:-2])
return self.strings
if root.left:
self.binaryTreePaths(root.left, string)
if root.right:
self.binaryTreePaths(root.right, string)
return self.strings
else:
return []
discuss solution
存在使用stack 和 queue的解法,递归没有使用全局变量
- BFS和DFS优先搜索算法(抽空去看)
# dfs + stack
def binaryTreePaths1(self, root):
if not root:
return []
res, stack = [], [(root, "")]
while stack:
node, ls = stack.pop()
if not node.left and not node.right:
res.append(ls+str(node.val))
if node.right:
stack.append((node.right, ls+str(node.val)+"->"))
if node.left:
stack.append((node.left, ls+str(node.val)+"->"))
return res
# bfs + queue
def binaryTreePaths2(self, root):
if not root:
return []
res, queue = [], collections.deque([(root, "")])
while queue:
node, ls = queue.popleft()
if not node.left and not node.right:
res.append(ls+str(node.val))
if node.left:
queue.append((node.left, ls+str(node.val)+"->"))
if node.right:
queue.append((node.right, ls+str(node.val)+"->"))
return res
# dfs recursively
def binaryTreePaths(self, root):
if not root:
return []
res = []
self.dfs(root, "", res)
return res
def dfs(self, root, ls, res):
if not root.left and not root.right:
res.append(ls+str(root.val))
if root.left:
self.dfs(root.left, ls+str(root.val)+"->", res)
if root.right:
self.dfs(root.right, ls+str(root.val)+"->", res)