Skip to content

Commit

Permalink
Merge pull request youngyangyang04#450 from betNevS/master
Browse files Browse the repository at this point in the history
增加 0098.验证二叉搜索树 go版 (增加中序遍历解题法)
  • Loading branch information
youngyangyang04 authored Jun 30, 2021
2 parents 56045e0 + f85d1c1 commit 1097fb5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 35 deletions.
22 changes: 22 additions & 0 deletions problems/0098.验证二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,28 @@ func isBST(root *TreeNode, min, max int) bool {
return isBST(root.Left, min, root.Val) && isBST(root.Right, root.Val, max)
}
```
```go
// 中序遍历解法
func isValidBST(root *TreeNode) bool {
// 保存上一个指针
var prev *TreeNode
var travel func(node *TreeNode) bool
travel = func(node *TreeNode) bool {
if node == nil {
return true
}
leftRes := travel(node.Left)
// 当前值小于等于前一个节点的值,返回false
if prev != nil && node.Val <= prev.Val {
return false
}
prev = node
rightRes := travel(node.Right)
return leftRes && rightRes
}
return travel(root)
}
```

JavaScript版本

Expand Down
64 changes: 29 additions & 35 deletions problems/0501.二叉搜索树中的众数.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func traversal(root *TreeNode,history map[int]int){
}
```

计数法BSL(此代码在执行代码里能执行,但提交后报错,不知为何,思路是对的)
计数法,不使用额外空间,利用二叉树性质,中序遍历

```go
/**
Expand All @@ -485,41 +485,35 @@ func traversal(root *TreeNode,history map[int]int){
* Right *TreeNode
* }
*/
var count,maxCount int //统计计数
func findMode(root *TreeNode) []int {
var result []int
var pre *TreeNode //前指针
if root.Left==nil&&root.Right==nil{
result=append(result,root.Val)
return result
}
traversal(root,&result,pre)
return result
}
func traversal(root *TreeNode,result *[]int,pre *TreeNode){//遍历统计
//如果BSL中序遍历相邻的两个节点值相同,则统计频率;如果不相同,依据BSL中序遍历排好序的性质,重新计数
if pre==nil{
count=1
}else if pre.Val==root.Val{
count++
}else {
count=1
}
//如果统计的频率等于最大频率,则加入结果集;如果统计的频率大于最大频率,更新最大频率且重新将结果加入新的结果集中
if count==maxCount{
*result=append(*result,root.Val)
}else if count>maxCount{
maxCount=count//重新赋值maxCount
*result=[]int{}//清空result中的内容
*result=append(*result,root.Val)
}
pre=root//保存上一个的节点
if root.Left!=nil{
traversal(root.Left,result,pre)
func findMode(root *TreeNode) []int {
res := make([]int, 0)
count := 1
max := 1
var prev *TreeNode
var travel func(node *TreeNode)
travel = func(node *TreeNode) {
if node == nil {
return
}
travel(node.Left)
if prev != nil && prev.Val == node.Val {
count++
} else {
count = 1
}
if count >= max {
if count > max && len(res) > 0 {
res = []int{node.Val}
} else {
res = append(res, node.Val)
}
max = count
}
prev = node
travel(node.Right)
}
if root.Right!=nil{
traversal(root.Right,result,pre)
}
travel(root)
return res
}
```

Expand Down
23 changes: 23 additions & 0 deletions problems/0530.二叉搜索树的最小绝对差.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,29 @@ func findMIn(root *TreeNode,res *[]int){
findMIn(root.Right,res)
}
```
```go
// 中序遍历的同时计算最小值
func getMinimumDifference(root *TreeNode) int {
// 保留前一个节点的指针
var prev *TreeNode
// 定义一个比较大的值
min := math.MaxInt64
var travel func(node *TreeNode)
travel = func(node *TreeNode) {
if node == nil {
return
}
travel(node.Left)
if prev != nil && node.Val - prev.Val < min {
min = node.Val - prev.Val
}
prev = node
travel(node.Right)
}
travel(root)
return min
}
```

JavaScript版本

Expand Down

0 comments on commit 1097fb5

Please sign in to comment.