dengxinyi vor 6 Jahren
Ursprung
Commit
870ef7f6ef
6 geänderte Dateien mit 156 neuen und 9 gelöschten Zeilen
  1. 2 2
      medium/105.go
  2. 2 2
      medium/106.go
  3. 50 5
      medium/109.go
  4. 42 0
      medium/113.go
  5. 34 0
      medium/114.go
  6. 26 0
      medium/116.js

+ 2 - 2
medium/105.go

@@ -23,10 +23,10 @@ func buildTreeByPreorderAndInorder(preorder []int, inorder []int) *TreeNode {
 		// Find the root index in inorder traversal
 	}
 	if idx != 0 { // If have left children
-		root.Left = buildTree(preorder[1:1+idx], inorder[0:idx])
+		root.Left = buildTree(preorder[1:1+idx], inorder[:idx])
 	}
 	if idx != nodeCnt-1 { // If have right children
-		root.Right = buildTree(preorder[idx+1:nodeCnt], inorder[idx+1:nodeCnt])
+		root.Right = buildTree(preorder[idx+1:], inorder[idx+1:])
 	}
 	return &root
 }

+ 2 - 2
medium/106.go

@@ -21,10 +21,10 @@ func buildTree(inorder []int, postorder []int) *TreeNode {
 	for idx = 0; idx < nodeCnt && inorder[idx] != root.Val; idx++ {
 	}
 	if idx != 0 {
-		root.Left = buildTree(inorder[0:idx], postorder[0:idx])
+		root.Left = buildTree(inorder[:idx], postorder[:idx])
 	}
 	if idx != nodeCnt-1 {
-		root.Right = buildTree(inorder[idx+1:nodeCnt], postorder[idx:nodeCnt-1])
+		root.Right = buildTree(inorder[idx+1:], postorder[idx:nodeCnt-1])
 	}
 	return &root
 }

+ 50 - 5
medium/109.go

@@ -15,15 +15,60 @@ package main
  *     Right *TreeNode
  * }
  */
+func sortedListToBSTOld(head *ListNode) *TreeNode {
+	list := make([]int, 0)
+	curr := head
+	for curr != nil {
+		list = append(list, curr.Val)
+		curr = curr.Next
+	}
+	return sortedArrayToBST(list)
+}
+
+func sortedArrayToBST(num []int) *TreeNode {
+	length := len(num)
+	if length == 0 {
+		return nil
+	} else if length == 1 {
+		return &TreeNode{num[0], nil, nil}
+	}
+	mid := length / 2
+	root := TreeNode{num[mid], nil, nil}
+	root.Left = sortedArrayToBST(num[:mid])
+	root.Right = sortedArrayToBST(num[mid+1:])
+	return &root
+}
+
 func sortedListToBST(head *ListNode) *TreeNode {
 	if head == nil {
 		return nil
+	} else if head.Next == nil {
+		return &TreeNode{head.Val, nil, nil}
 	}
-	root := TreeNode{head.Val, nil, nil}
-	return &root
+	return listToBSTHelper(head, nil)
 }
 
-func main() {
-	printTree(sortedListToBST((toLinkedList(
-		[]int{-10, -3, 0, 5, 9}))))
+func listToBSTHelper(head, tail *ListNode) *TreeNode {
+	if head == tail {
+		return nil
+	}
+	slow := head
+	fast := head
+	for fast != tail && fast.Next != tail {
+		slow = slow.Next
+		fast = fast.Next.Next
+	}
+	root := TreeNode{slow.Val, nil, nil}
+	root.Left = listToBSTHelper(head, slow)
+	root.Right = listToBSTHelper(slow.Next, tail)
+	return &root
 }
+
+// func main() {
+// 	printTree(sortedListToBST((toLinkedList(
+// 		[]int{-10, -3, 0, 5, 9}))))
+// 	printTree(sortedListToBST((toLinkedList(
+// 		[]int{}))))
+// 	printTree(sortedListToBST((toLinkedList(
+// 		[]int{1, 2}))))
+// }

+ 42 - 0
medium/113.go

@@ -0,0 +1,42 @@
+package main
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func pathSum(root *TreeNode, sum int) [][]int {
+	result := make([][]int, 0)
+	path := make([]int, 0)
+	pathSumHelper(&result, &path, root, sum)
+	return result
+}
+
+func pathSumHelper(result *[][]int, path *[]int, root *TreeNode, sum int) {
+	if root == nil {
+		return
+	}
+	*path = append(*path, root.Val)
+	if root.Left == nil && root.Right == nil && root.Val == sum {
+		newPath := make([]int, len(*path)) // Deep copy & pointer
+		copy(newPath, *path)               // Important!!!
+		*result = append(*result, newPath)
+	}
+	pathSumHelper(result, path, root.Left, sum-root.Val)
+	pathSumHelper(result, path, root.Right, sum-root.Val)
+	*path = (*path)[:len(*path)-1]
+}
+
+// func main() {
+// 	//         3
+// 	//        / \
+// 	//       7  -1
+// 	//      /\   /\
+// 	//     1  5 9  4
+// 	root1 := sortedArrayToBST([]int{1, 7, 5, 3, 9, -1, 4})
+// 	fmt.Println(pathSum(root1, 11))
+// 	fmt.Println(pathSum(root1, 12))
+// }

+ 34 - 0
medium/114.go

@@ -0,0 +1,34 @@
+package main
+
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func flatten(root *TreeNode) { // Inorder flatten
+	prev := (*TreeNode)(nil)
+	flattenHelper(root, &prev)
+}
+
+// Reverse order of the inorder traversal
+// root -> left -> right to right -> left -> root
+func flattenHelper(root *TreeNode, prev **TreeNode) {
+	if root == nil {
+		return
+	}
+	flattenHelper(root.Right, prev)
+	flattenHelper(root.Left, prev)
+	root.Left = nil
+	root.Right = *prev
+	*prev = root
+}
+
+func main() {
+	root1 := sortedArrayToBST([]int{3, 2, 4, 1, 6, 5, 7})
+	printTree(root1)
+	flatten(root1)
+	printTree(root1)
+}

+ 26 - 0
medium/116.js

@@ -0,0 +1,26 @@
+/**
+ * Definition for binary tree with next pointer.
+ * function TreeLinkNode(val) {
+ *     this.val = val;
+ *     this.left = this.right = this.next = null;
+ * }
+ */
+
+function TreeLinkNode(val) {
+    this.val = val
+    this.left = this.right = this.next = null
+}
+
+/**
+ * @param {TreeLinkNode} root
+ * @return {void} Do not return anything, modify tree in-place instead.
+ */
+var connect = function(root) {
+    
+}
+
+function __main__() {
+    
+}
+
+__main__()