255. Verify Preorder Sequence in Binary Search Tree
来源:互联网 发布:ubuntu命令行模式乱码 编辑:程序博客网 时间:2024/05/22 14:07
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?
Solution 1 : Divide and conquer
public class Solution { public boolean verifyPreorder(int[] preorder) { return verify(preorder,0,preorder.length - 1); } public boolean verify(int[] preorder, int start, int end){ if(start >= end) return true; int p = preorder[start]; int i = start + 1; while(i <= end){ if(preorder[i] > p) break; i++; } int j = i; while(j <= end){ if(preorder[j] < p) return false; j++; } return verify(preorder,start + 1, i - 1) && verify(preorder,i,end); }}Solution 2 : from https://discuss.leetcode.com/topic/21217/java-o-n-and-o-1-extra-space
Kinda simulate the traversal, keeping a stack of nodes (just their values) of which we're still in the left subtree. If the next number is smaller than the last stack value, then we're still in the left subtree of all stack nodes, so just push the new one onto the stack. But before that, pop all smaller ancestor values, as we must now be in their right subtrees (or even further, in the right subtree of an ancestor). Also, use the popped values as a lower bound, since being in their right subtree means we must never come across a smaller number anymore.
public boolean verifyPreorder(int[] preorder) { int low = Integer.MIN_VALUE; Stack<Integer> path = new Stack(); for (int p : preorder) { if (p < low) return false; while (!path.empty() && p > path.peek()) low = path.pop(); path.push(p); } return true;}
- Verify Preorder Sequence in Binary Search Tree
- Verify Preorder Sequence in Binary Search Tree
- LeetCode 255. Verify Preorder Sequence in Binary Search Tree
- 255. Verify Preorder Sequence in Binary Search Tree
- Leetcode 255. Verify Preorder Sequence in Binary Search Tree
- 255. Verify Preorder Sequence in Binary Search Tree
- [Leetcode]Verify Preorder Sequence in Binary Search Tree
- LeetCode 题解(264) : Verify Preorder Sequence in Binary Search Tree:
- *LeetCode-Verify Preorder Sequence in Binary Search Tree
- [LeetCode255]Verify Preorder Sequence in Binary Search Tree
- Leetcode 255 Verify Preorder Sequence in Binary Search Tree
- [leetcode] 255. Verify Preorder Sequence in Binary Search Tree 解题报告
- Leetcode 255. Verify Preorder Sequence in Binary Search Tree (Medium) (cpp)
- [Leetcode] 255. Verify Preorder Sequence in Binary Search Tree 解题报告
- LeetCode 255. Verify Preorder Sequence in Binary Search Tree(检查二叉搜索树的前序遍历)
- [leetcode 255] Verify Preorder Sequence in Binary Search Tree ---先序遍历验证二叉搜索树
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- makefile 分析
- 《Effective C++》构造、析构、赋值运算
- MeritMS价值管理—添加、提交、审核和排名
- 深度 | 谷歌新人李飞飞:击碎玻璃天花板的华裔女科学家
- cf 359c Prime Number
- 255. Verify Preorder Sequence in Binary Search Tree
- 软件工程师,你必须知道的20个常识
- map和struct的多重嵌套
- ImageView随机显示图片
- HTML和JavaScript
- poj 1182 食物链 (并查集 经典)
- arm简介
- spark mllib实现 广告点击率预测
- 矩阵快速幂