-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbinary_tree_paths.go
72 lines (58 loc) · 1.49 KB
/
binary_tree_paths.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"strconv"
"strings"
)
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
type nodePath struct {
node *TreeNode
prevPath []int
}
func binaryTreePaths(root *TreeNode) []string {
res := make([]string, 0)
stack := make([]nodePath, 0)
stack = append(stack, nodePath{root, []int{}})
var sb strings.Builder
for len(stack) > 0 {
cur := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if cur.node.Left == nil && cur.node.Right == nil {
for _, n := range cur.prevPath {
sb.WriteString(strconv.Itoa(n))
sb.WriteString("->")
}
sb.WriteString(strconv.Itoa(cur.node.Val))
res = append(res, sb.String())
sb.Reset()
}
if cur.node.Left != nil {
stack = append(stack, nodePath{cur.node.Left, append(cur.prevPath, cur.node.Val)})
}
if cur.node.Right != nil {
stack = append(stack, nodePath{cur.node.Right, append(cur.prevPath, cur.node.Val)})
}
}
return res
}
// func binaryTreePaths(root *TreeNode) []string {
// return binaryTreePathsWithString(root, "")
// }
// func binaryTreePathsWithString(node *TreeNode, str string) []string {
// if node == nil {
// return []string{}
// }
// if node.Left == nil && node.Right == nil {
// str += fmt.Sprint(node.Val)
// return []string{str}
// }
// str += fmt.Sprintf("%v->", node.Val)
// return append(
// binaryTreePathsWithString(node.Left, str),
// binaryTreePathsWithString(node.Right, str)...,
// )
// }