-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path257.py
38 lines (26 loc) · 818 Bytes
/
257.py
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
36
37
38
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
# @param {TreeNode} root
# @return {string[]}
def binaryTreePaths(self, root):
l = []
def build_line(r, line):
if r.left is None and r.right is None:
l.append(line)
return
if r.left:
build_line(r.left, "%s->%s" % (line, r.left.val))
if r.right:
build_line(r.right, "%s->%s" % (line, r.right.val))
if root is None:
return l
build_line(root, "%s" % root.val)
return l