IT评测·应用市场-qidao123.com技术社区

标题: Golang | Leetcode Golang题解之第257题二叉树的所有路径 [打印本页]

作者: 慢吞云雾缓吐愁    时间: 2024-7-23 20:02
标题: Golang | Leetcode Golang题解之第257题二叉树的所有路径
题目:

题解:
  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企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4