/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode}root * @return {boolean} */ var isValidBST = function(root) { var list = inorderTraversal(root) var base = list.join(',') return base === [...new Set(list)].sort((a, b) => (a - b)).join(',') };
// 获取中序遍历 var inorderTraversal = function (root) { if (root === null) return [] return [...inorderTraversal(root.left), root.val, ...inorderTraversal(root.right)] };