Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 21d6f28

Browse files
Merge pull request vJechsmayr#501 from matheusphalves/feature/matheusphalves
Added 0257_Binary_Tree_Paths.py
2 parents f91e113 + 0dbf96d commit 21d6f28

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎LeetCode/0257_Binary_Tree_Paths.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def __init__(self):
9+
self.listOfPaths = [] #this list will save all paths and return
10+
11+
def binaryTreePaths(self, root: TreeNode, path="") -> List[str]:
12+
if(root==None):
13+
return [] #nothing to return (it's a empty node) ...
14+
15+
path += " " + str(root.val)#actual node value becomes a 'element route'
16+
17+
if(root.left==None and root.right==None):
18+
self.listOfPaths.append(path[1:].replace(" ", "->"))#question output format
19+
20+
else:
21+
self.binaryTreePaths(root.left, path) #scan by route in left node
22+
self.binaryTreePaths(root.right, path)#scan by route in right node
23+
return self.listOfPaths #return all paths
24+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /