We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 中等
Related Topics: 树, 深度优先搜索, 二叉搜索树, 二叉树
给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
root
有效 二叉搜索树定义如下:
示例 1:
输入:root = [2,1,3] 输出:true
示例 2:
输入:root = [5,1,4,null,null,3,6] 输出:false 解释:根节点的值是 5 ,但是右子节点的值是 4 。
提示:
Language: JavaScript
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */ /** * @param {TreeNode} root * @return {boolean} */ // 栈 var isValidBST = function(root) { let stack = [] let inorder = -Infinity while (root || stack.length) { while (root) { stack.push(root) root = root.left } root = stack.pop() if (root.val <= inorder) { return false } inorder = root.val root = root.right } return true }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
98. 验证二叉搜索树
Description
Difficulty: 中等
Related Topics: 树, 深度优先搜索, 二叉搜索树, 二叉树
给你一个二叉树的根节点
root
,判断其是否是一个有效的二叉搜索树。有效 二叉搜索树定义如下:
示例 1:
示例 2:
提示:
Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: