[Swift]LeetCode255.驗證二叉搜索樹的先序序列 $ Verify Preorder Sequence in Binary Search Tree

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-cmbgdgzn-mb.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.git

You may assume each number in the sequence is unique.github

Follow up:
Could you do it using only constant space complexity?數組


給定一個數字數組,驗證它是不是二進制搜索樹的正確的預排序遍歷序列。微信

您能夠假定序列中的每一個數字都是惟一的。spa

進階:code

你能只用恆定的空間複雜性來作嗎?htm


 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 }
相關文章
相關標籤/搜索