力扣—>二叉树的所有路径
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
var paths []string
func binaryTreePaths(root *TreeNode) []string {
paths = []string{}
dfs(root,"")
return paths
}
func dfs(node *TreeNode, path string){
/*
if node==nil{
path += "null"
paths = append(paths, path)
return
}
这样输出的结果会把空的也输出来:[1->2->null 1->2->5 1->null]
*/
if node==nil{
return
}
path += strconv.Itoa(node.Val)
if node.Left ==nil && node.Right ==nil{
paths = append(paths,path)
return
}
path += "->"
dfs(node.Left,path)
dfs(node.Right,path)
}