dengxinyi před 7 roky
rodič
revize
ef36ca5cc9
2 změnil soubory, kde provedl 51 přidání a 4 odebrání
  1. 10 0
      hard/115.go
  2. 41 4
      hard/99.go

+ 10 - 0
hard/115.go

@@ -0,0 +1,10 @@
+package main
+
+func numDistinct(s string, t string) int {
+	return 0
+}
+
+func main() {
+	println(numDistinct("rabbbit", "rabbit"), 3)
+	println(numDistinct("babgbag", "bag"), 5)
+}

+ 41 - 4
hard/99.go

@@ -1,5 +1,9 @@
 package main
 
+import (
+	"math"
+)
+
 /**
  * Definition for a binary tree node.
  * type TreeNode struct {
@@ -9,9 +13,42 @@ package main
  * }
  */
 func recoverTree(root *TreeNode) {
-
+	if root == nil {
+		return
+	}
+	var swap1, swap2 *TreeNode
+	prev := &TreeNode{math.MinInt32, nil, nil}
+	var recurse func(*TreeNode)
+	recurse = func(root *TreeNode) {
+		if root == nil {
+			return
+		}
+		recurse(root.Left)
+		// It's kinda strange, but there's an example:
+		// 1 2 6 4 5 3
+		// The first element must be the larger one, so pick prev;
+		// the second element is the smaller one, pick root.
+		if swap1 == nil && root.Val <= prev.Val {
+			swap1 = prev
+		}
+		if swap1 != nil && root.Val <= prev.Val {
+			swap2 = root
+		}
+		prev = root
+		recurse(root.Right)
+	}
+	recurse(root)
+	swap1.Val, swap2.Val = swap2.Val, swap1.Val
 }
 
-func main() {
-
-}
+// func main() {
+// 	tree := toBinaryTree(nil)
+// 	recoverTree(tree)
+// 	printTree(tree)
+// 	tree = toBinaryTree(newInt(1), newInt(3), nil, nil, newInt(2))
+// 	recoverTree(tree)
+// 	printTree(tree)
+// 	tree = toBinaryTree(newInt(3), newInt(1), newInt(4), nil, nil, newInt(2))
+// 	recoverTree(tree)
+// 	printTree(tree)
+// }