-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path257_binaryTreePaths.py
More file actions
36 lines (30 loc) · 1.04 KB
/
257_binaryTreePaths.py
File metadata and controls
36 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 257. Binary Tree Paths
# Refer to Nick White's Video https://www.youtube.com/watch?v=H2D4HcVZq_g
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
def traverse(root, current, result):
current += "->" + str(root.val)
if not root.left and not root.right:
result.append(current)
return
if root.left:
traverse(root.left, current, result)
if root.right:
traverse(root.right, current, result)
class Solution:
def binaryTreePaths(self, root: TreeNode) -> List[str]:
result = []
if not root:
return result
current_path = str(root.val)
if not root.left and not root.right:
result.append(current_path)
if root.left:
traverse(root.left, current_path, result)
if root.right:
traverse(root.right, current_path, result)
return result