255. Verify Preorder Sequence in Binary Search Tree

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?

Hide Company Tags

Zenefits

Show Tags

Show Similar Problems

/* Stack time O(n)space O(n)
public class Solution {
    public boolean verifyPreorder(int[] preorder) {
        //stack preorder root -left - right  先降序后升序
        Stack<Integer> stack = new Stack<>();
        int min = Integer.MIN_VALUE;
        for(int i = 0; i < preorder.length; i ++){
            if(preorder[i] < min)
                return false;
            while(!stack.isEmpty() && stack.peek() < preorder[i]){
                min = stack.pop();
            }
            stack.push(preorder[i]);
        }
        return true;
    }
}*/

public class Solution {
    public boolean verifyPreorder(int[] preorder) {
        int i = -1;
        int min = Integer.MIN_VALUE;
        for(int num : preorder){
            if(num < min)
                return false;
            while(i >= 0 &&  preorder[i] < num){
                min = preorder[i--];
            }
            preorder[++i] = num;
        }
        return true;
    }
}

Follow up 参考 https://segmentfault.com/a/1190000003874375

如何验证中序序列?A:中序序列是有序的,只要验证其是否升序的就行了。

如何验证后序序列?

后序序列的顺序是left - right - root,而先序的顺序是root - left - right。我们同样可以用本题的方法解,不过是从数组的后面向前面遍历,因为root在后面了。而且因为从后往前看是先遇到right再遇到left,所以我们要记录的是限定的最大值,而不再是最小值,栈pop的条件也变成pop所有比当前数大得数。栈的增长方向也是从高向低了。

public static boolean verifyPostorder(int[] preorder) {
            int i = preorder.length;
            int max = Integer.MAX_VALUE;
            for(int j = preorder.length -1; j >=0; j--){
                if(preorder[j] > max)
                    return false;
                while(i < preorder.length &&  preorder[i] < preorder[j]){
                    max = preorder[i++];
                }
                preorder[--i] = preorder[j];
            }
            return true;
        }
时间: 2024-10-10 02:33:02

255. Verify Preorder Sequence in Binary Search Tree的相关文章

Leetcode 255. Verify Preorder Sequence in Binary Search Tree

验证一个list是不是一个BST的preorder traversal sequence. 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

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

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? 给一个数组,验证是否为一个二叉搜索树的

LeetCode Verify Preorder Sequence in Binary Search Tree

原题链接在这里:https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/ 题目: 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 u

Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree

Verify Preorder Sequence in Binary Search Tree \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 on

[Swift Weekly Contest 127]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal

Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has

A1043. Is It a Binary Search Tree (25)

Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

1043. Is It a Binary Search Tree (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1043. Is It a Binary Search Tree (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than

1043. Is It a Binary Search Tree (25)

时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue (二叉树建立方法) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than th