★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10225416.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.
You may assume each number in the sequence is unique.
Follow up:
Could you do it using only constant space complexity?
给定一个数字数组,验证它是否是二进制搜索树的正确的预排序遍历序列。
您可以假定序列中的每个数字都是唯一的。
进阶:
你能只用恒定的空间复杂性来做吗?
1 class Solution { 2 func verifyPreorder(_ preorder:[Int]) -> Bool{ 3 var preorder = preorder 4 var low:Int = Int.min 5 var i:Int = -1 6 for a in preorder 7 { 8 if a < low 9 { 10 return false 11 } 12 while(i >= 0 && a > preorder[i]) 13 { 14 low = preorder[i] 15 i -= 1 16 } 17 i += 1 18 preorder[i] = a 19 } 20 return true 21 } 22 }