Browse Source

update helper.go and so on

dengxinyi 6 years ago
parent
commit
16d968ac49
5 changed files with 113 additions and 14 deletions
  1. 25 7
      medium/105.go
  2. 39 0
      medium/106.go
  3. 29 0
      medium/109.go
  4. 3 3
      medium/93.go
  5. 17 4
      medium/helper.go

+ 25 - 7
medium/105.go

@@ -8,16 +8,34 @@ package main
  *     Right *TreeNode
  * }
  */
-func buildTree(preorder []int, inorder []int) *TreeNode {
-	if len(preorder) == 0 || len(preorder) != len(inorder) {
+func buildTreeByPreorderAndInorder(preorder []int, inorder []int) *TreeNode {
+	// func buildTree(preorder []int, inorder []int) *TreeNode {
+	nodeCnt := len(preorder)
+	if nodeCnt == 0 || nodeCnt != len(inorder) {
 		return nil
 	}
 	root := TreeNode{preorder[0], nil, nil}
+	if nodeCnt == 1 { // No children
+		return &root
+	}
+	var idx int
+	for idx = 0; idx < nodeCnt && inorder[idx] != root.Val; idx++ {
+		// Find the root index in inorder traversal
+	}
+	if idx != 0 { // If have left children
+		root.Left = buildTree(preorder[1:1+idx], inorder[0:idx])
+	}
+	if idx != nodeCnt-1 { // If have right children
+		root.Right = buildTree(preorder[idx+1:nodeCnt], inorder[idx+1:nodeCnt])
+	}
 	return &root
 }
 
-func main() {
-	printTree(buildTree(
-		[]int{3, 9, 20, 15, 7},
-		[]int{9, 3, 15, 20, 7}))
-}
+// func main() {
+// 	printTree(buildTree(
+// 		[]int{1, 2, 4, 5, 3, 6, 7},
+// 		[]int{4, 2, 5, 1, 6, 3, 7}))
+// 	printTree(buildTree(
+// 		[]int{1, 2},
+// 		[]int{1, 2}))
+// }

+ 39 - 0
medium/106.go

@@ -0,0 +1,39 @@
+package main
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func buildTree(inorder []int, postorder []int) *TreeNode {
+	nodeCnt := len(inorder)
+	if nodeCnt == 0 || nodeCnt != len(postorder) {
+		return nil
+	}
+	root := TreeNode{postorder[nodeCnt-1], nil, nil}
+	if nodeCnt == 1 {
+		return &root
+	}
+	var idx int
+	for idx = 0; idx < nodeCnt && inorder[idx] != root.Val; idx++ {
+	}
+	if idx != 0 {
+		root.Left = buildTree(inorder[0:idx], postorder[0:idx])
+	}
+	if idx != nodeCnt-1 {
+		root.Right = buildTree(inorder[idx+1:nodeCnt], postorder[idx:nodeCnt-1])
+	}
+	return &root
+}
+
+// func main() {
+// 	printTree(buildTree(
+// 		[]int{4, 2, 5, 1, 6, 3, 7},
+// 		[]int{4, 5, 2, 6, 7, 3, 1}))
+// 	printTree(buildTree(
+// 		[]int{9, 3, 15, 20, 7},
+// 		[]int{9, 15, 7, 20, 3}))
+// }

+ 29 - 0
medium/109.go

@@ -0,0 +1,29 @@
+package main
+
+/**
+ * Definition for singly-linked list.
+ * type ListNode struct {
+ *     Val int
+ *     Next *ListNode
+ * }
+ */
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func sortedListToBST(head *ListNode) *TreeNode {
+	if head == nil {
+		return nil
+	}
+	root := TreeNode{head.Val, nil, nil}
+	return &root
+}
+
+func main() {
+	printTree(sortedListToBST((toLinkedList(
+		[]int{-10, -3, 0, 5, 9}))))
+}

+ 3 - 3
medium/93.go

@@ -2,11 +2,11 @@ package main
 
 func isValidPart(part string) bool {
 	switch len(part) {
-	case 1:
+	case 1: // \d
 		return true
-	case 2:
+	case 2: // [^0]\d
 		return part[0] != '0'
-	case 3:
+	case 3: // 1\d{2} || 2[0-4]\d || 25[1-5]
 		if part[0] != '2' {
 			return part[0] == '1'
 		}

+ 17 - 4
medium/helper.go

@@ -38,14 +38,27 @@ 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 := ""
 	for curr != nil {
-		str += strconv.FormatInt(int64(curr.Val), 10) + " "
+		print(strconv.FormatInt(int64(curr.Val), 10), " ")
 		curr = curr.Next
 	}
-	return str
+	println()
 }
 
 func printTree(root *TreeNode) { // Level order traversal