mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
@ -336,8 +336,26 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
# Definition for a binary tree node.
|
||||||
|
# class TreeNode:
|
||||||
|
# def __init__(self, val=0, left=None, right=None):
|
||||||
|
# self.val = val
|
||||||
|
# self.left = left
|
||||||
|
# self.right = right
|
||||||
|
//递归法
|
||||||
|
class Solution:
|
||||||
|
def isValidBST(self, root: TreeNode) -> bool:
|
||||||
|
res = [] //把二叉搜索树按中序遍历写成list
|
||||||
|
def buildalist(root):
|
||||||
|
if not root: return
|
||||||
|
buildalist(root.left) //左
|
||||||
|
res.append(root.val) //中
|
||||||
|
buildalist(root.right) //右
|
||||||
|
return res
|
||||||
|
buildalist(root)
|
||||||
|
return res == sorted(res) and len(set(res)) == len(res) //检查list里的数有没有重复元素,以及是否按从小到大排列
|
||||||
|
```
|
||||||
Go:
|
Go:
|
||||||
```Go
|
```Go
|
||||||
import "math"
|
import "math"
|
||||||
@ -365,4 +383,4 @@ func isBST(root *TreeNode, min, max int) bool {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
Reference in New Issue
Block a user