-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution257.go
More file actions
49 lines (43 loc) · 960 Bytes
/
solution257.go
File metadata and controls
49 lines (43 loc) · 960 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package solution257
import (
"strconv"
)
// ============================================================================
// 257. Binary Tree Paths
// URL: https://leetcode.com/problems/binary-tree-paths/
// ============================================================================
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
var list []string
func binaryTreePaths(root *TreeNode) []string {
list = []string{}
if root == nil {
return []string{}
}
addPath(root, "")
return list
}
func addPath(root *TreeNode, s string) {
if root.Left == nil && root.Right == nil {
s += strconv.Itoa(root.Val)
list = append(list, s)
return
}
if root.Left == nil {
s += strconv.Itoa(root.Val) + "->"
addPath(root.Right, s)
return
}
if root.Right == nil {
s += strconv.Itoa(root.Val) + "->"
addPath(root.Left, s)
return
}
v := strconv.Itoa(root.Val)
s += v + "->"
addPath(root.Left, s)
addPath(root.Right, s)
}