首页 > 其他 > 详细

递归与二叉树_leetcode257

时间:2019-03-17 14:20:11      阅读:121      评论:0      收藏:0      [点我收藏+]
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None


# 这是从底向上构建路径
# 从上到下构建路径则需要将path 作为参数传递到下一层 待完成 20190305
class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""

res = []
if not root:
return res
if not root.left and not root.right:
res.append(str(root.val))

leftPaths = self.binaryTreePaths(root.left)

for i in range(len(leftPaths)):
res.append(str(root.val) + "->" +leftPaths[i])

rightPaths = self.binaryTreePaths(root.right)
for i in range(len(rightPaths)):
res.append(str(root.val) + "->" + rightPaths[i])

return res

递归与二叉树_leetcode257

原文:https://www.cnblogs.com/lux-ace/p/10546811.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!