题目
思路
借用中序遍历,把节点数值放进一个数组里,然后比较这个数组是否单调递增
代码
/**
* 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 num = [];
inorder(root);
function inorder(root){
if(root == null){
return;
}
inorder(root.left);
num.push(root.val);
inorder(root.right);
}
//判断是否不符合顺序
for(let i = 1; i < num.length; i++){
if(num[i] <= num[i - 1])return false;
}
return true;
};