Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5c52e57

Browse files
committed
add 98
1 parent 6ef8aa1 commit 5c52e57

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
# 0098.validate-binary-search-tree
3+
```text
4+
5+
给定一个二叉树,判断其是否是一个有效的二叉搜索树。
6+
7+
假设一个二叉搜索树具有如下特征:
8+
9+
节点的左子树只包含小于当前节点的数。
10+
节点的右子树只包含大于当前节点的数。
11+
所有左子树和右子树自身必须也是二叉搜索树。
12+
13+
示例 1:
14+
15+
输入:
16+
2
17+
/ \
18+
1 3
19+
输出: true
20+
示例 2:
21+
22+
输入:
23+
5
24+
/ \
25+
1 4
26+
/ \
27+
3 6
28+
输出: false
29+
解释: 输入为: [5,1,4,null,null,3,6]。
30+
根节点的值为 5 ,但是其右子节点值为 4 。
31+
32+
来源:力扣(LeetCode)
33+
链接:https://leetcode-cn.com/problems/validate-binary-search-tree
34+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
35+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package tree
2+
3+
import (
4+
"fmt"
5+
"github.com/itcuihao/leetcode-go/structure"
6+
)
7+
8+
func Run() {
9+
root := structure.BinaryTree98
10+
b := isValidBST(root)
11+
fmt.Println(b)
12+
}
13+
14+
/**
15+
* Definition for a binary tree node.
16+
* type TreeNode struct {
17+
* Val int
18+
* Left *TreeNode
19+
* Right *TreeNode
20+
* }
21+
*/
22+
func isValidBST(root *structure.TreeNode) bool {
23+
res := traverse(root)
24+
if len(res) <= 1 {
25+
return true
26+
}
27+
tmp := res[0]
28+
for i := 1; i < len(res); i++ {
29+
if res[i] <= tmp {
30+
return false
31+
}
32+
tmp = res[i]
33+
}
34+
return true
35+
}
36+
37+
// 中序遍历获取有序数组
38+
func traverse(root *structure.TreeNode) []int {
39+
res := make([]int, 0)
40+
if root == nil {
41+
return res
42+
}
43+
res = append(res, traverse(root.Left)...)
44+
res = append(res, root.Val)
45+
res = append(res, traverse(root.Right)...)
46+
return res
47+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
package tree
3+
4+
import "testing"
5+
6+
func TestRun(t *testing.T) {
7+
Run()
8+
}

‎structure/init.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var BinaryTree = &TreeNode{
5555
},
5656
},
5757
}
58+
5859
var BinaryTree101 = &TreeNode{
5960
Val: 1,
6061
Left: &TreeNode{
@@ -134,3 +135,22 @@ var ListNodeCycle = &ListNode{
134135
},
135136
},
136137
}
138+
139+
var BinaryTree98 = &TreeNode{
140+
Val: 2,
141+
Left: &TreeNode{
142+
Val: 1,
143+
},
144+
Right: &TreeNode{
145+
Val: 3,
146+
},
147+
//Right: &TreeNode{
148+
// Val: 7,
149+
// Left: &TreeNode{
150+
// Val: 6,
151+
// },
152+
// Right: &TreeNode{
153+
// Val: 8,
154+
// },
155+
//},
156+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /