mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
增加 0257.二叉树的所有路径 go 版
This commit is contained in:
@ -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.递归版本
|
||||
|
Reference in New Issue
Block a user