-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path257.go
More file actions
43 lines (39 loc) · 920 Bytes
/
Copy path257.go
File metadata and controls
43 lines (39 loc) · 920 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
package main
import "strconv"
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func binaryTreePaths(root *TreeNode) []string {
if root == nil {
return []string{}
}
if root.Left == nil && root.Right == nil {
return []string{strconv.Itoa(root.Val)}
}
var ret []string
var recur func(*TreeNode, string)
recur = func(root *TreeNode, s string) {
if s != "" {
s += "->" + strconv.Itoa(root.Val)
} else {
s = strconv.Itoa(root.Val)
}
if root.Left == nil && root.Right == nil {
ret = append(ret, s)
return
}
if root.Left != nil {
recur(root.Left, s)
}
if root.Right != nil {
recur(root.Right, s)
}
}
recur(root, "")
return ret
}