mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
41 lines
786 B
Go
41 lines
786 B
Go
package leetcode
|
|
|
|
import (
|
|
"strconv"
|
|
)
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// TreeNode define
|
|
type TreeNode = structures.TreeNode
|
|
|
|
/**
|
|
* 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{}
|
|
}
|
|
res := []string{}
|
|
if root.Left == nil && root.Right == nil {
|
|
return []string{strconv.Itoa(root.Val)}
|
|
}
|
|
tmpLeft := binaryTreePaths(root.Left)
|
|
for i := 0; i < len(tmpLeft); i++ {
|
|
res = append(res, strconv.Itoa(root.Val)+"->"+tmpLeft[i])
|
|
}
|
|
tmpRight := binaryTreePaths(root.Right)
|
|
for i := 0; i < len(tmpRight); i++ {
|
|
res = append(res, strconv.Itoa(root.Val)+"->"+tmpRight[i])
|
|
}
|
|
return res
|
|
}
|