[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 驗證二叉搜尋樹的先序序列

來源:互聯網
上載者:User

標籤:有序數組   exit   .com   blog   btree   ali   title   code   etc   

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?

給一個數組,驗證是否為一個二叉搜尋樹的先序遍曆出的序列。

二叉樹的特點是:左<根<右,如果用中序遍曆得到的結果就是有序數組,而先序遍曆的結果就不是有序數組。

Python:

# Time:  O(n)# Space: O(h)class Solution2:    # @param {integer[]} preorder    # @return {boolean}    def verifyPreorder(self, preorder):        low = float("-inf")        path = []        for p in preorder:            if p < low:                return False            while path and p > path[-1]:                low = path[-1]                path.pop()            path.append(p)        return True

Python:  

# Time:  O(n)# Space: O(1)class Solution:    # @param {integer[]} preorder    # @return {boolean}    def verifyPreorder(self, preorder):        low, i = float("-inf"), -1        for p in preorder:            if p < low:                return False            while i >= 0 and p > preorder[i]:                low = preorder[i]                i -= 1            i += 1            preorder[i] = p        return True

C++:

// Time:  O(n)// Space: O(h)class Solution2 {public:    bool verifyPreorder(vector<int>& preorder) {        int low = INT_MIN;        stack<int> path;        for (auto& p : preorder) {            if (p < low) {                return false;            }            while (!path.empty() && p > path.top()) {                // Traverse to its right subtree now.                // Use the popped values as a lower bound because                // we shouldn‘t come across a smaller number anymore.                low = path.top();                path.pop();            }            path.emplace(p);        }        return true;    }};

C++:

// Time:  O(n)// Space: O(1)class Solution {public:    bool verifyPreorder(vector<int>& preorder) {        int low = INT_MIN, i = -1;        for (auto& p : preorder) {            if (p < low) {                return false;            }            while (i >= 0 && p > preorder[i]) {                low = preorder[i--];            }            preorder[++i] = p;        }        return true;    }};

  

  

類似題目:

[LeetCode] 144. Binary Tree Preorder Traversal 二叉樹的先序遍曆

[LeetCode] 98. Validate Binary Search Tree 驗證二叉搜尋樹

  

 

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 驗證二叉搜尋樹的先序序列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.