Leetcode 257. Binary Tree Paths
题目
Given a binary tree, return all root-to-leaf paths.
Note: A leaf is a node with no children.
解法 1:DFS
参考129解法的前半部分
class Solution:
def binaryTreePaths(self, root: TreeNode) -> List[str]:
def create_path(root,curr_path):
if not root.right and not root.left:
if not curr_path:
pathlist.append(str(root.val))
else:
pathlist.append(curr_path+'->'+str(root.val))
if root.left:
if not curr_path:
create_path(root.left,str(root.val))
else:
create_path(root.left,curr_path+'->'+str(root.val))
if root.right:
if not curr_path:
create_path(root.right,str(root.val))