This repository was archived by the owner on Sep 22, 2021. It is now read-only.

Description
Description of the Problem
Given a binary tree, return all root-to-leaf paths.
Note: A leaf is a node with no children.
Example:
Input:
1
/ \
2 3
\
5
Output: ["1->2->5", "1->3"]
Explanation: All root-to-leaf paths are: 1->2->5, 1->3
Code
class Solution:
def __init__(self):
self.listOfPaths = [] #this list will save all paths and return
def binaryTreePaths(self, root: TreeNode, path="") -> List[str]:
if(root==None):
return [] #nothing to return (it's a empty node) ...
path += " " + str(root.val)#actual node value becomes a 'element route'
if(root.left==None and root.right==None):
self.listOfPaths.append(path[1:].replace(" ", "->"))#question output format
else:
self.binaryTreePaths(root.left, path) #scan by route in left node
self.binaryTreePaths(root.right, path)#scan by route in right node
return self.listOfPaths #return all paths
Link To The LeetCode Problem
LeetCode