题解 | #判断是不是二叉搜索树#

判断是不是二叉搜索树

http://www.nowcoder.com/practice/a69242b39baf45dea217815c7dedb52b

package main
import . "nc_tools"
/*
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param root TreeNode类 
 * @return bool布尔型
*/
// 取int最小值
var cache = -1 << (32 - 1)
var b = true

func isValidBST(root *TreeNode) bool {
    if root == nil {
        return true
    }
    helperNode(root)
    return b
}

func helperNode(root *TreeNode) {
    if root == nil {
        return
    }
    helperNode(root.Left)
    if root.Val < cache {
        b = false
        return
    }
    cache = root.Val
    helperNode(root.Right)
}

全部评论

相关推荐

能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务