Skip to content

Commit a18df38

Browse files
committed
feat(solutions): add validate_binary_search_tree
1 parent 34756c8 commit a18df38

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ If you like it then you can put a :star:**Star** on it.
3434
3535
| # | **Problem** & **Solution** | Difficulty | Single Repetition Duration | LeetCode Run Time |
3636
| ---: | :----- | :----------: | ----------: | ----------: |
37+
|[98][Solutions-98]|[Validate Binary Search Tree][Solutions-98-Home]|Medium|[21.0 ns/op][Solutions-98-Code] / [8 test cases][Solutions-98-Test]|8 ms|
3738
|[96][Solutions-96]|[Unique Binary Search Trees][Solutions-96-Home]|Medium|[39.1 ns/op][Solutions-96-Code] / [6 test cases][Solutions-96-Test]|0 ms|
3839
|[95][Solutions-95]|[Unique Binary Search Trees II][Solutions-95-Home]|Medium|[259 ns/op][Solutions-95-Code] / [3 test cases][Solutions-95-Test]|48 ms|
3940
|[94][Solutions-94]|[Binary Tree Inorder Traversal][Solutions-94-Home]|Medium|[119 ns/op][Solutions-94-Code] / [3 test cases][Solutions-94-Test]|0 ms|
@@ -153,6 +154,10 @@ Welcome to report bugs, suggest ideas and discuss on [issues page](https://githu
153154
## License
154155
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FWindomZ%2Fleetcode.go.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FWindomZ%2Fleetcode.go?ref=badge_large)
155156

157+
[Solutions-98]:https://leetcode.com/problems/validate-binary-search-tree/
158+
[Solutions-98-Home]:solutions/validate_binary_search_tree/
159+
[Solutions-98-Code]:solutions/validate_binary_search_tree/validatebinarysearchtree.go
160+
[Solutions-98-Test]:solutions/validate_binary_search_tree/validatebinarysearchtree_test.go#L62
156161
[Solutions-96]:https://leetcode.com/problems/unique-binary-search-trees/
157162
[Solutions-96-Home]:solutions/unique_binary_search_trees/
158163
[Solutions-96-Code]:solutions/unique_binary_search_trees/uniquebinarysearchtrees.go
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 解法要点
2+
1. BST(binary search trees)是左叶值小于节点值,右叶值大于节点值
3+
1. 运用DP解法,迭代处理各种情况
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [98. Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/)
2+
3+
## Description
4+
5+
Given a binary tree, determine if it is a valid binary search tree (BST).
6+
7+
Assume a BST is defined as follows:
8+
9+
* The left subtree of a node contains only nodes with keys **less than** the node's key.
10+
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
11+
* Both the left and right subtrees must also be binary search trees.
12+
13+
**Example 1:**
14+
```
15+
2
16+
/ \
17+
1 3
18+
```
19+
20+
Binary tree `[2,1,3]`, return true.
21+
22+
**Example 2:**
23+
```
24+
1
25+
/ \
26+
2 3
27+
```
28+
29+
Binary tree `[1,2,3]`, return false.
30+
31+
## Solution
32+
- [Code](validatebinarysearchtree.go)
33+
- [Testing](validatebinarysearchtree_test.go)
34+
35+
## Note
36+
- [中文](NOTE_Ch-zh.md)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package validatebinarysearchtree
2+
3+
import "math"
4+
5+
// TreeNode definition for a binary tree node.
6+
type TreeNode struct {
7+
Val int
8+
Left *TreeNode
9+
Right *TreeNode
10+
}
11+
12+
func isValidBST(root *TreeNode) bool {
13+
return validBST(root, math.MinInt64, math.MaxInt64)
14+
}
15+
16+
func validBST(root *TreeNode, low, high int64) bool {
17+
if root == nil {
18+
return true
19+
}
20+
val := int64(root.Val)
21+
if val >= high || val <= low {
22+
return false
23+
}
24+
return validBST(root.Left, low, val) && validBST(root.Right, val, high)
25+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package validatebinarysearchtree
2+
3+
import (
4+
"testing"
5+
6+
"github.com/WindomZ/testify/assert"
7+
)
8+
9+
func Test_isValidBST(t *testing.T) {
10+
assert.True(t, isValidBST(nil))
11+
assert.False(t, isValidBST(&TreeNode{
12+
Val: 1,
13+
Left: &TreeNode{
14+
Val: 1,
15+
},
16+
}))
17+
assert.True(t, isValidBST(&TreeNode{
18+
Val: 1,
19+
Right: &TreeNode{
20+
Val: 2,
21+
Right: &TreeNode{Val: 3},
22+
},
23+
}))
24+
assert.True(t, isValidBST(&TreeNode{
25+
Val: 2,
26+
Left: &TreeNode{Val: 1},
27+
Right: &TreeNode{Val: 3},
28+
}))
29+
assert.False(t, isValidBST(&TreeNode{
30+
Val: 1,
31+
Right: &TreeNode{
32+
Val: 3,
33+
Right: &TreeNode{Val: 2},
34+
},
35+
}))
36+
assert.False(t, isValidBST(&TreeNode{
37+
Val: 1,
38+
Left: &TreeNode{
39+
Val: 3,
40+
Right: &TreeNode{Val: 2},
41+
},
42+
}))
43+
assert.False(t, isValidBST(&TreeNode{
44+
Val: 1,
45+
Left: &TreeNode{Val: 2},
46+
Right: &TreeNode{Val: 3},
47+
}))
48+
assert.False(t, isValidBST(&TreeNode{
49+
Val: 2,
50+
Left: &TreeNode{Val: 3},
51+
Right: &TreeNode{Val: 1},
52+
}))
53+
assert.False(t, isValidBST(&TreeNode{
54+
Val: 2,
55+
Right: &TreeNode{
56+
Val: 3,
57+
Right: &TreeNode{Val: 1},
58+
},
59+
}))
60+
}
61+
62+
func Benchmark_isValidBST(b *testing.B) {
63+
b.StopTimer()
64+
b.ReportAllocs()
65+
b.StartTimer()
66+
b.RunParallel(func(pb *testing.PB) {
67+
for pb.Next() {
68+
isValidBST(nil)
69+
isValidBST(&TreeNode{
70+
Val: 1,
71+
Left: &TreeNode{
72+
Val: 1,
73+
},
74+
})
75+
isValidBST(&TreeNode{
76+
Val: 1,
77+
Right: &TreeNode{
78+
Val: 2,
79+
Right: &TreeNode{Val: 3},
80+
},
81+
})
82+
isValidBST(&TreeNode{
83+
Val: 2,
84+
Left: &TreeNode{Val: 1},
85+
Right: &TreeNode{Val: 3},
86+
})
87+
isValidBST(&TreeNode{
88+
Val: 1,
89+
Right: &TreeNode{
90+
Val: 3,
91+
Right: &TreeNode{Val: 2},
92+
},
93+
})
94+
isValidBST(&TreeNode{
95+
Val: 1,
96+
Left: &TreeNode{
97+
Val: 3,
98+
Right: &TreeNode{Val: 2},
99+
},
100+
})
101+
isValidBST(&TreeNode{
102+
Val: 1,
103+
Left: &TreeNode{Val: 2},
104+
Right: &TreeNode{Val: 3},
105+
})
106+
isValidBST(&TreeNode{
107+
Val: 2,
108+
Left: &TreeNode{Val: 3},
109+
Right: &TreeNode{Val: 1},
110+
})
111+
isValidBST(&TreeNode{
112+
Val: 2,
113+
Right: &TreeNode{
114+
Val: 3,
115+
Right: &TreeNode{Val: 1},
116+
},
117+
})
118+
}
119+
})
120+
}

0 commit comments

Comments
 (0)