Golang | Leetcode Golang题解之第257题二叉树的所有路径

打印 上一主题 下一主题

主题 1541|帖子 1541|积分 4623

题目:

题解:
  1. func binaryTreePaths(root *TreeNode) []string {
  2.     paths := []string{}
  3.     if root == nil {
  4.         return paths
  5.     }
  6.     nodeQueue := []*TreeNode{}
  7.     pathQueue := []string{}
  8.     nodeQueue = append(nodeQueue, root)
  9.     pathQueue = append(pathQueue, strconv.Itoa(root.Val))
  10.     for i := 0; i < len(nodeQueue); i++ {
  11.         node, path := nodeQueue[i], pathQueue[i]
  12.         if node.Left == nil && node.Right == nil {
  13.             paths = append(paths, path)
  14.             continue
  15.         }
  16.         if node.Left != nil {
  17.             nodeQueue = append(nodeQueue, node.Left)
  18.             pathQueue = append(pathQueue, path + "->" + strconv.Itoa(node.Left.Val))
  19.         }
  20.         if node.Right != nil {
  21.             nodeQueue = append(nodeQueue, node.Right)
  22.             pathQueue = append(pathQueue, path + "->" + strconv.Itoa(node.Right.Val))
  23.         }
  24.     }
  25.     return paths
  26. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

慢吞云雾缓吐愁

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表