Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ae6abaa

Browse files
authored
Create Print_paths_in_Binary_Tree.py
1 parent bc1c0fe commit ae6abaa

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Function to print all the paths of a Binary Tree | O(N) Time & Space
2+
3+
def get_paths(root, path, paths):
4+
if root is None:
5+
return
6+
path.append(root.val)
7+
get_paths(root.left, path, paths)
8+
get_paths(root.right, path, paths)
9+
10+
11+
if root.left is None and root.right is None:
12+
paths.append(list(path))
13+
14+
# Pop here because, we're changing direction here.
15+
if path:
16+
path.pop()
17+
18+
paths = []
19+
get_paths(root, [], paths)

0 commit comments

Comments
 (0)