|
@@ -1,3 +1,7 @@
|
|
|
+import (
|
|
|
+ "strconv"
|
|
|
+)
|
|
|
+
|
|
|
/**
|
|
|
* Definition for a binary tree node.
|
|
|
* type TreeNode struct {
|
|
@@ -6,6 +10,23 @@
|
|
|
* Right *TreeNode
|
|
|
* }
|
|
|
*/
|
|
|
-func binaryTreePaths(root *TreeNode) []string {
|
|
|
-
|
|
|
+func binaryTreePaths(root *TreeNode) (paths []string) {
|
|
|
+ if root == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ traversalPaths(root, "", &paths)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func traversalPaths(node *TreeNode, path string, paths *[]string) {
|
|
|
+ path += "->" + strconv.Itoa(node.Val)
|
|
|
+ if node.Left != nil {
|
|
|
+ traversalPaths(node.Left, path, paths)
|
|
|
+ }
|
|
|
+ if node.Right != nil {
|
|
|
+ traversalPaths(node.Right, path, paths)
|
|
|
+ }
|
|
|
+ if node.Left == nil && node.Right == nil {
|
|
|
+ *paths = append(*paths, path[2:])
|
|
|
+ }
|
|
|
}
|