https://leetcode.com/problems/validate-binary-search-tree/
测试发现,将方法写在class作用域内的内存消耗远远小于放在func内,以后用递归思路实现的swift算法尽量还是将func移出来。二是有点奇怪,我测试python和swift速度,一般来说算法都是python较快,不知道为何,讲道理不应该。

class Solution {
    func isValidBST(_ root: TreeNode?) -> Bool {
        return isValidBST(root, .min, .max)
    }
    func isValidBST(_ node: TreeNode?, _ min: Int, _ max: Int) -> Bool {
        guard let n = node else { return true }
        guard n.val < max && n.val > min else { return false }
        return isValidBST(n.left, Swift.max(min, .min), n.val)
            && isValidBST(n.right, n.val, Swift.min(max, .max))
    }
}

Runtime: 44 ms, faster than 100.00% of Swift online submissions for Validate Binary Search Tree.
Memory Usage: 19.5 MB, less than 76.92% of Swift online submissions for Validate Binary Search Tree.