2025年6月5日 星期四 乙巳(蛇)年 三月初九 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > 数据结构与算法

算法:98 Validate BST

时间:12-14来源:作者:点击数:16

按照前序遍历得到数组,然后看是否是有序的,时间优于 5%,空间优于 10%。

  • var isValidBST = function(root) {
  • if(root == null || (root.left == null && root.right == null)) return true;
  • function flatTree(root) {
  • if(root == null || root == undefined) return [];
  • return [...flatTree(root.left), root.val, ...flatTree(root.right)];
  • }
  • let flatedList = flatTree(root);
  • for(let i = 1; i< flatedList.length; i++) {
  • if(flatedList[i] <= flatedList[i - 1]) return false;
  • }
  • return true;
  • };
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐