-
标签:
树
-
难度:
简单
- 题目描述
- 我的解法
考虑递归函数的返回如何构成问题的解,不难解决。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
result = []
if not root:
return result
if not root.left and not root.right:
result.append(str(root.val))
lefts = self.binaryTreePaths(root.left)
rights = self.binaryTreePaths(root.right)
for left in lefts:
result.append(str(root.val) + '->' + left)
for right in rights:
result.append(str(root.val) + '->' + right)
return result
- 其他解法
暂略。
网友评论