增加 0257.二叉树的所有路径 go 版

This commit is contained in:
NevS
2021-06-20 00:45:47 +08:00
committed by GitHub
parent e5e8755888
commit a85e41093b

View File

@ -350,6 +350,29 @@ class Solution:
```
Go
```go
func binaryTreePaths(root *TreeNode) []string {
res := make([]string, 0)
var travel func(node *TreeNode, s string)
travel = func(node *TreeNode, s string) {
if node.Left == nil && node.Right == nil {
v := s + strconv.Itoa(node.Val)
res = append(res, v)
return
}
s = s + strconv.Itoa(node.Val) + "->"
if node.Left != nil {
travel(node.Left, s)
}
if node.Right != nil {
travel(node.Right, s)
}
}
travel(root, "")
return res
}
```
JavaScript:
1.递归版本