Skip to content

Commit 3a64bb5

Browse files
committed
feat: finish 1008
1 parent 2086eb3 commit 3a64bb5

File tree

15 files changed

+165
-18
lines changed

15 files changed

+165
-18
lines changed

.vscode/launch.json

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,34 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7-
{
8-
"type": "node",
9-
"request": "launch",
10-
"name": "node",
11-
"runtimeExecutable": "node",
12-
"program": "${fileDirname}/index.js",
13-
"restart": true,
14-
"console": "integratedTerminal",
15-
"internalConsoleOptions": "neverOpen"
16-
},
17-
{
18-
"name": "leetcode",
19-
"type": "go",
20-
"request": "launch",
21-
"mode": "test",
22-
"program": "${fileDirname}"
23-
}]
7+
{
8+
"type": "java",
9+
"name": "CodeLens (Launch) - Example",
10+
"request": "launch",
11+
"mainClass": "Example"
12+
},
13+
{
14+
"type": "java",
15+
"name": "CodeLens (Launch) - sort",
16+
"request": "launch",
17+
"mainClass": "sort"
18+
},
19+
{
20+
"type": "node",
21+
"request": "launch",
22+
"name": "node",
23+
"runtimeExecutable": "node",
24+
"program": "${fileDirname}/index.js",
25+
"restart": true,
26+
"console": "integratedTerminal",
27+
"internalConsoleOptions": "neverOpen"
28+
},
29+
{
30+
"name": "leetcode",
31+
"type": "go",
32+
"request": "launch",
33+
"mode": "test",
34+
"program": "${fileDirname}"
35+
}
36+
]
2437
}

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"python.linting.enabled": true
2+
"python.linting.enabled": true,
3+
"java.format.settings.profile": "GoogleStyle",
34
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# [1008. Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)
2+
3+
## 2019/08/31
4+
5+
### 题目 💗[medium]
6+
7+
Return the root node of a binary search tree that matches the given preorder traversal.
8+
9+
(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val. Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.)
10+
11+
Example 1:
12+
13+
```bash
14+
Input: [8,5,1,7,10,12]
15+
Output: [8,5,10,1,7,null,12]
16+
```
17+
18+
Note:
19+
20+
- 1 <= preorder.length <= 100
21+
- The values of preorder are distinct.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package bstFromPreorder
2+
3+
import "github.com/pengliheng/leetcode/Helper"
4+
5+
type TreeNode = Helper.TreeNode
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* type TreeNode struct {
10+
* Val int
11+
* Left *TreeNode
12+
* Right *TreeNode
13+
* }
14+
*/
15+
func bstFromPreorder(preorder []int) *TreeNode {
16+
if len(preorder) == 0 {
17+
return nil
18+
}
19+
root := &TreeNode{Val: preorder[0]}
20+
for i, e := range preorder {
21+
if i == 0 {
22+
continue
23+
}
24+
current := root
25+
for {
26+
if current.Val > e {
27+
if current.Left == nil {
28+
current.Left = &TreeNode{Val: e}
29+
break
30+
} else {
31+
current = current.Left
32+
}
33+
} else {
34+
if current.Right == nil {
35+
current.Right = &TreeNode{Val: e}
36+
break
37+
} else {
38+
current = current.Right
39+
}
40+
}
41+
}
42+
}
43+
return root
44+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package bstFromPreorder
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pengliheng/leetcode/Helper"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var tcs = []struct {
12+
N1 []int
13+
ans *TreeNode
14+
}{
15+
{
16+
[]int{8, 5, 1, 7, 10, 12},
17+
Helper.Ints2TreeNode([]int{8, 5, 10, 1, 7, -1 << 63, 12}),
18+
},
19+
}
20+
21+
func Test_bitwiseComplement(t *testing.T) {
22+
ast := assert.New(t)
23+
for _, tc := range tcs {
24+
ast.Equal(tc.ans, bstFromPreorder(tc.N1), "输入:%v", tc)
25+
}
26+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)