dengxinyi 6 年之前
父節點
當前提交
18728b0abb
共有 9 個文件被更改,包括 114 次插入76 次删除
  1. 6 6
      easy/101.go
  2. 6 6
      easy/104.go
  3. 0 18
      easy/108.go
  4. 6 6
      easy/110.go
  5. 6 6
      easy/111.go
  6. 5 5
      easy/21.go
  7. 41 12
      easy/helper.go
  8. 3 5
      hard/25.go
  9. 41 12
      hard/helper.go

+ 6 - 6
easy/101.go

@@ -1,11 +1,11 @@
 package main
 
-// TreeNode ...
-type TreeNode struct {
-	Val   int
-	Left  *TreeNode
-	Right *TreeNode
-}
+// // TreeNode ...
+// type TreeNode struct {
+// 	Val   int
+// 	Left  *TreeNode
+// 	Right *TreeNode
+// }
 
 func isSymmetricIter(lNode *TreeNode, rNode *TreeNode) bool {
 	if lNode == nil || rNode == nil {

+ 6 - 6
easy/104.go

@@ -7,12 +7,12 @@ package main
 // 	Right *TreeNode
 // }
 
-func maxInt(x, y int) int {
-	if x > y {
-		return x
-	}
-	return y
-}
+// func maxInt(x, y int) int {
+// 	if x > y {
+// 		return x
+// 	}
+// 	return y
+// }
 
 func maxDepthIter(curr *TreeNode, depth int) int {
 	if curr == nil {

+ 0 - 18
easy/108.go

@@ -1,23 +1,5 @@
 package main
 
-import (
-	"fmt"
-)
-
-// print tree
-func printTree(root *TreeNode) {
-	if root == nil {
-		return
-	}
-	fmt.Print(root.Val, " ")
-	if root.Left != nil {
-		printTree(root.Left)
-	}
-	if root.Right != nil {
-		printTree(root.Right)
-	}
-}
-
 // type TreeNode struct {
 // 	Val   int
 // 	Left  *TreeNode

+ 6 - 6
easy/110.go

@@ -13,12 +13,12 @@ package main
 // 	return y
 // }
 
-func abs(x int) int {
-	if x < 0 {
-		return -x
-	}
-	return x
-}
+// func abs(x int) int {
+// 	if x < 0 {
+// 		return -x
+// 	}
+// 	return x
+// }
 
 func treeDepth(root *TreeNode, depth int) int {
 	if root == nil {

+ 6 - 6
easy/111.go

@@ -6,12 +6,12 @@ package main
 // 	Right *TreeNode
 // }
 
-func minInt(x, y int) int {
-	if x < y {
-		return x
-	}
-	return y
-}
+// func minInt(x, y int) int {
+// 	if x < y {
+// 		return x
+// 	}
+// 	return y
+// }
 
 func minDepthIter(root *TreeNode, depth int) int {
 	if root.Left == nil && root.Right == nil {

+ 5 - 5
easy/21.go

@@ -1,10 +1,10 @@
 package main
 
-// ListNode ...
-type ListNode struct {
-	Val  int
-	Next *ListNode
-}
+// // ListNode ...
+// type ListNode struct {
+// 	Val  int
+// 	Next *ListNode
+// }
 
 func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
 	// special cases: [] []; [0] []; [] [0]

+ 41 - 12
easy/helper.go

@@ -1,7 +1,7 @@
 package main
 
 import (
-	"fmt"
+	"strconv"
 )
 
 // ListNode ...
@@ -38,25 +38,54 @@ func minInt(x, y int) int {
 	return y
 }
 
-func list2str(head *ListNode) string {
+func toLinkedList(num []int) *ListNode {
+	length := len(num)
+	if length == 0 {
+		return nil
+	}
+	head := ListNode{num[0], nil}
+	curr := &head
+	for i := 1; i < length; i++ {
+		curr.Next = &ListNode{num[i], nil}
+		curr = curr.Next
+	}
+	return &head
+}
+
+func printList(head *ListNode) {
 	curr := head
-	str := make([]rune, 0)
 	for curr != nil {
-		str = append(str, rune(curr.Val+'0'))
+		print(strconv.FormatInt(int64(curr.Val), 10), " ")
 		curr = curr.Next
 	}
-	return string(str)
+	println()
 }
 
-func printTree(root *TreeNode) {
+func printTree(root *TreeNode) { // Level order traversal
 	if root == nil {
+		println("nil")
 		return
 	}
-	if root.Left != nil {
-		printTree(root.Left)
-	}
-	if root.Right != nil {
-		printTree(root.Right)
+	queue := make([]*TreeNode, 0) // Important!
+	queue = append(queue, root)
+	for len(queue) != 0 {
+		curr := queue[0]
+		queue = queue[1:] // Dequeue
+		if curr == nil {
+			print("nil ")
+			continue
+		}
+		print(curr.Val, " ")
+		if curr.Left != nil {
+			queue = append(queue, curr.Left)
+		} else if curr.Right != nil {
+			queue = append(queue, nil)
+		}
+		if curr.Right != nil {
+			queue = append(queue, curr.Right)
+		} else if curr.Left != nil {
+			queue = append(queue, nil)
+		}
 	}
-	fmt.Print(root.Val, " ")
+	println()
 }

+ 3 - 5
hard/25.go

@@ -1,7 +1,5 @@
 package main
 
-import "fmt"
-
 /**
  * Definition for singly-linked list.
  * type ListNode struct {
@@ -61,9 +59,9 @@ func main() {
 	n3 := ListNode{3, &n4}
 	n2 := ListNode{2, &n3}
 	n1 := ListNode{1, &n2}
-	fmt.Println(list2str(&n1))
-	fmt.Println(list2str(reverseKGroup(&n1, 5)))
+	printList(&n1)
+	printList(reverseKGroup(&n1, 5))
 	// 0
 	n0 := ListNode{0, nil}
-	fmt.Println(list2str(reverseKGroup(&n0, 1)))
+	printList(reverseKGroup(&n0, 1))
 }

+ 41 - 12
hard/helper.go

@@ -1,7 +1,7 @@
 package main
 
 import (
-	"fmt"
+	"strconv"
 )
 
 // ListNode ...
@@ -38,25 +38,54 @@ func minInt(x, y int) int {
 	return y
 }
 
-func list2str(head *ListNode) string {
+func toLinkedList(num []int) *ListNode {
+	length := len(num)
+	if length == 0 {
+		return nil
+	}
+	head := ListNode{num[0], nil}
+	curr := &head
+	for i := 1; i < length; i++ {
+		curr.Next = &ListNode{num[i], nil}
+		curr = curr.Next
+	}
+	return &head
+}
+
+func printList(head *ListNode) {
 	curr := head
-	str := make([]rune, 0)
 	for curr != nil {
-		str = append(str, rune(curr.Val+'0'))
+		print(strconv.FormatInt(int64(curr.Val), 10), " ")
 		curr = curr.Next
 	}
-	return string(str)
+	println()
 }
 
-func printTree(root *TreeNode) {
+func printTree(root *TreeNode) { // Level order traversal
 	if root == nil {
+		println("nil")
 		return
 	}
-	if root.Left != nil {
-		printTree(root.Left)
-	}
-	if root.Right != nil {
-		printTree(root.Right)
+	queue := make([]*TreeNode, 0) // Important!
+	queue = append(queue, root)
+	for len(queue) != 0 {
+		curr := queue[0]
+		queue = queue[1:] // Dequeue
+		if curr == nil {
+			print("nil ")
+			continue
+		}
+		print(curr.Val, " ")
+		if curr.Left != nil {
+			queue = append(queue, curr.Left)
+		} else if curr.Right != nil {
+			queue = append(queue, nil)
+		}
+		if curr.Right != nil {
+			queue = append(queue, curr.Right)
+		} else if curr.Left != nil {
+			queue = append(queue, nil)
+		}
 	}
-	fmt.Print(root.Val, " ")
+	println()
 }