leetcode-257 二叉树的所有路径 2015年03月01日 Tree 留言 257. 二叉树的所有路径 难度: 简单 考察递归, 此题为 Google 的面试题之一 123456789101112131415161718192021222324252627282930313233/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */func binaryTreePaths(root *TreeNode) []string { var res []string if root == nil { return res } 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} 文章目录 原文链接: https://dashen.tech/2015/03/01/leetcode-257-二叉树的所有路径/ 版权声明: 转载请注明出处.