File tree Expand file tree Collapse file tree 2 files changed +92
-0
lines changed
Expand file tree Collapse file tree 2 files changed +92
-0
lines changed Original file line number Diff line number Diff line change 1+ # Definition for a binary tree node.
2+ # class TreeNode(object):
3+ # def __init__(self, x):
4+ # self.val = x
5+ # self.left = None
6+ # self.right = None
7+
8+ class Solution (object ):
9+ def binaryTreePaths (self , root ):
10+ """
11+ :type root: TreeNode
12+ :rtype: List[str]
13+ """
14+ self .paths = []
15+ self .visit (root , "" )
16+ return self .paths
17+
18+ def visit (self , node , current ):
19+ if not node :
20+ return
21+ if current :
22+ current += "->"
23+ current += str (node .val )
24+ if not node .left and not node .right :
25+ self .paths .append (current )
26+ else :
27+ self .visit (node .left , current )
28+ self .visit (node .right , current )
29+
Original file line number Diff line number Diff line change 1+ class Solution (object ):
2+ def numberToWords (self , num ):
3+ """
4+ :type num: int
5+ :rtype: str
6+ """
7+ self .d = {
8+ 1000000000 : "Billion" ,
9+ 1000000 :"Million" ,
10+ 1000 : "Thousand" ,
11+ 100 : "Hundred" ,
12+ 90 : "Ninty" ,
13+ 80 : "Eighty" ,
14+ 70 : "Seventy" ,
15+ 60 : "Sixty" ,
16+ 50 : "Fifty" ,
17+ 40 : "Forty" ,
18+ 30 : "Thirty" ,
19+ 20 : "Twenty" ,
20+ 19 : "Nineteen" ,
21+ 18 : "Eighteen" ,
22+ 17 : "Seventeen" ,
23+ 16 : "Sixteen" ,
24+ 15 : "Fifteen" ,
25+ 14 : "Fourteen" ,
26+ 13 : "Thirteen" ,
27+ 12 : "Twelve" ,
28+ 11 : "Eleven" ,
29+ 10 : "Ten" ,
30+ 9 : "Nine" ,
31+ 8 : "Eight" ,
32+ 7 : "Seven" ,
33+ 6 :"Six" ,
34+ 5 :"Five" ,
35+ 4 : "Four" ,
36+ 3 : "Three" ,
37+ 2 : "Two" ,
38+ 1 : "One" ,
39+ }
40+ self .keys = sorted (self .d .keys (), reverse = True )
41+
42+ if num == 0 :
43+ return "Zero"
44+ return self .helper (num )
45+
46+ def helper (self , num ):
47+ s = ""
48+ i = 0
49+ while num :
50+ while self .keys [i ]> num :
51+ i += 1
52+ k = self .keys [i ]
53+ if s :
54+ s += " "
55+ if k >= 100 :
56+ n = num / k
57+ s1 = self .helper (n )
58+ s += s1 + " " + self .d [k ]
59+ num = num % k
60+ else :
61+ num -= k
62+ s += self .d [k ]
63+ return s
You can’t perform that action at this time.
0 commit comments