|
| 1 | +package hello |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "log" |
| 6 | +) |
| 7 | + |
| 8 | +type TreeNode struct { |
| 9 | + Val int |
| 10 | + Left *TreeNode |
| 11 | + Right *TreeNode |
| 12 | +} |
| 13 | + |
| 14 | +func buildTree(preorder []int, inorder []int) *TreeNode { |
| 15 | + if len(preorder) == 1 { |
| 16 | + return &TreeNode{ |
| 17 | + Val: preorder[0], |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + center, _ := findCenterWithValue(preorder[0], inorder) |
| 22 | + |
| 23 | + head := TreeNode{ |
| 24 | + Val: inorder[center], |
| 25 | + } |
| 26 | + |
| 27 | + execute(&head, center, inorder) |
| 28 | + |
| 29 | + return &head |
| 30 | +} |
| 31 | + |
| 32 | +func findCenterWithValue(key int, inorder []int) (int, error) { |
| 33 | + for i, n := range inorder { |
| 34 | + if n == key { |
| 35 | + return i, nil |
| 36 | + } |
| 37 | + } |
| 38 | + return 0, errors.New("not found") |
| 39 | +} |
| 40 | + |
| 41 | +func findCenter(slice []int) int { |
| 42 | + c := len(slice) / 2 |
| 43 | + return c |
| 44 | +} |
| 45 | + |
| 46 | +func getSplit(center int, inorder []int, currentVal int) ([]int, []int) { |
| 47 | + if len(inorder) == 1 { |
| 48 | + if inorder[0] == currentVal { |
| 49 | + return nil, nil |
| 50 | + } |
| 51 | + return []int{inorder[0]}, nil |
| 52 | + } |
| 53 | + if len(inorder) == 2 { |
| 54 | + if inorder[0] == currentVal { |
| 55 | + return nil, []int{inorder[1]} |
| 56 | + } |
| 57 | + if inorder[1] == currentVal { |
| 58 | + return []int{inorder[0]}, nil |
| 59 | + } |
| 60 | + return []int{inorder[0]}, []int{inorder[1]} |
| 61 | + } |
| 62 | + return inorder[:center], inorder[center+1:] |
| 63 | +} |
| 64 | + |
| 65 | +func execute(currentNode *TreeNode, center int, slice []int) { |
| 66 | + left, right := getSplit(center, slice, currentNode.Val) |
| 67 | + |
| 68 | + if len(left) == 1 { |
| 69 | + currentNode.Left = &TreeNode{Val: left[0]} |
| 70 | + } else if len(left) == 2 { |
| 71 | + currentNode.Left = &TreeNode{Val: left[0]} |
| 72 | + currentNode.Left.Right = &TreeNode{Val: left[1]} |
| 73 | + } else if len(left) > 2 { |
| 74 | + lc := findCenter(left) |
| 75 | + currentNode.Left = &TreeNode{Val: left[lc]} |
| 76 | + |
| 77 | + if len(left) > 2 { |
| 78 | + execute(currentNode.Left, lc, left) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if len(right) == 1 { |
| 83 | + currentNode.Right = &TreeNode{Val: right[0]} |
| 84 | + } else if len(right) == 2 { |
| 85 | + currentNode.Right = &TreeNode{Val: right[0]} |
| 86 | + currentNode.Right.Left = &TreeNode{Val: right[1]} |
| 87 | + } else if len(right) > 2 { |
| 88 | + rc := findCenter(right) |
| 89 | + currentNode.Right = &TreeNode{Val: right[rc]} |
| 90 | + |
| 91 | + if len(right) > 2 { |
| 92 | + execute(currentNode.Right, rc, right) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func print(currentNode *TreeNode) { |
| 98 | + log.Println(currentNode.Val) |
| 99 | + if currentNode.Left != nil { |
| 100 | + print(currentNode.Left) |
| 101 | + } |
| 102 | + if currentNode.Right != nil { |
| 103 | + print(currentNode.Right) |
| 104 | + } |
| 105 | +} |
0 commit comments