-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTreePaths.py
More file actions
28 lines (25 loc) · 877 Bytes
/
binaryTreePaths.py
File metadata and controls
28 lines (25 loc) · 877 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/binary-tree-paths/
# Author: Miao Zhang
# Date: 2021-01-29
# 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
class Solution:
def binaryTreePaths(self, root: TreeNode) -> List[str]:
res = []
self.dfs(root, '', res)
return res
def dfs(self, node: TreeNode, path: str, res: List[str]) -> None:
if not node: return
if not node.left and not node.right:
res.append(path + str(node.val))
return
if node.left:
self.dfs(node.left, path + str(node.val) + '->', res)
if node.right:
self.dfs(node.right, path + str(node.val) + '->', res)