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 unique.

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

题解:

Method 1 利用stack模拟preorder. stack中放的是左子树. 遇到比stack top还要大, 说明遇到了以top 为root的右子树,把左子树连同root 都pop出来.

low是当前的lower bound, pop出来说明左边扫完了,不可能出现更小的了,所以更新low. 若是遇到比low还小就说明不是preorder.

Time Complexity: O(n). Space: O(logn).

Method 2 是在array本身上实现stack的功能.

Time Complexity: O(n). Space: O(1).

AC Java:

 1 public class Solution {
 2     public boolean verifyPreorder(int[] preorder) {
 3         /*
 4         //Method 1
 5         int low = Integer.MIN_VALUE;
 6         Stack<Integer> stk = new Stack<Integer>();
 7         for(int num : preorder){
 8             if(num < low){
 9                 return false;
10             }
11             while(!stk.isEmpty() && stk.peek() < num){
12                low = stk.pop();
13             }
14             stk.push(num);
15         }
16         return true;
17         */
18         int index = -1;
19         int low = Integer.MIN_VALUE;
20         for(int num : preorder){
21             if(num < low){
22                 return false;
23             }
24             while(index >= 0 && preorder[index] < num){
25                 low = preorder[index--];
26             }
27             preorder[++index] = num;
28         }
29         return true;
30     }
31 }
时间: 2024-10-24 23:04:44

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

[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 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

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 Z

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

[leetcode]Convert Sorted List to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意:将一条排序好的链表转换为二叉查找树,二叉查找树需要平衡. 解题思路:两个思路:一,可以使用快慢指针来找到中间的那个节点,然后将这个节点作为树根,并分别递归这个节点左右两边的链表产生左右子树,这样的好处是不需要使用额外的空间,坏处是代码不够整洁.二,将排序好的链表的每个节点的值存入一个数组中,这样就和http://www.cnblog

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class

LeetCode: Convert Sorted List to Binary Search Tree [109]

[题目] Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. [题意] 将一个有序链表转换成平衡二叉树 [思路] 思路跟Convert Sorted Array to Binary Search Tree完全一样 [代码] /** * Definition for singly-linked list. * struct List

[LeetCode] Convert Sorted List to Binary Search Tree(分治)

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 方法:为了使BST高度平衡,要找链表中的中值作为当前根节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

LeetCode: Convert Sorted Array to Binary Search Tree [108]

[题目] Given an array where elements are sorted in ascending order, convert it to a height balanced BST. [题意] 给定一个已排序的数组(不存在重复元素),将它转换成一棵平衡二叉搜索树. [思路] 由于平衡二叉树要求左右子树的高度差绝对值相遇等于1,也就是说左右子树尽可能包含相同数目节点. 则使用二分法来解本题即可. [代码] /** * Definition for binary tree *