Construct BST from given preorder traversal
Given preorder traversal of a binary search tree, construct the BST.
For example, if the given traversal is {10, 5, 1, 7, 40, 50}, then the output should be root of following tree.
10
/ \
5 40
/ \ \
1 7 50
We have discussed O(n^2) and O(n) recursive solutions in the previous post. Following is a stack based iterative solution that works in O(n) time.
1. Create an empty stack.
2. Make the first value as root. Push it to the stack.
3. Keep on popping while the stack is not empty and the next value is greater than stack’s top value. Make this value as the right child of the last popped node. Push the new node to the stack.
4. If the next value is less than the stack’s top value, make this value as the left child of the stack’s top node. Push the new node to the stack.
5. Repeat steps 2 and 3 until there are items remaining in pre[].
This can be done in constant time with two way:
//Construct BST from given preorder traversal
class BSTNode{
public TreeNode tree;
public Integer min;
public Integer max;
public BSTNode(TreeNode tree, int min, int max){
this.tree = tree;
this.min = min;
this.max = max;
}
} //Iterative:
public static TreeNode preorderToBST(int[] preorder){
if(preorder == null || preorder.length == 0) return null;
LinkedList<BSTNode> stack = new LinkedList<BSTNode>();
int len = preorder.length;
TreeNode result = new TreeNode(preorder[0]);
BSTNode root = new BSTNode(result, Integer.MIN_VALUE, Integer.MAX_VALUE);
stack.push(root);
for(int i = 1; i < len; i ++){
TreeNode cur = new TreeNode(preorder[i]);
while(!stack.isEmpty()){
BSTNode tmp = stack.peek();
if(tmp.min < cur.val && cur.val < tmp.tree.val){//left child
tmp.tree.left = cur;
stack.push(new BSTNode(cur, tmp.min, tmp.tree.val));
break;
}else if(tmp.tree.val < cur.val && cur.val < tmp.max){//right child
tmp.tree.right = cur;
stack.push(new BSTNode(cur, tmp.tree.val, tmp.max));
break;
}else if(cur.val > tmp.max){//not this treenode's child
stack.pop();
}else{
System.out.println("Error happens! This is not a valid preorder traersal array. ");
return null;
}
}
}
return result;
} //Recrusive:
public int pos = 0;
public static TreeNode preorderToBST(int[] preorder, int min, int max){
if(preorder == null || preorder.length == 0 || pos == preorder.length) return null;
int val = preorder[pos];
if(min < val && val < max){
TreeNode root = new TreeNode(val);
pos ++;
root.left = preorderToBST(preorder, min, val);
root.right = preorderToBST(preorder, val, max);
}
}
Construct BST from given preorder traversal的更多相关文章
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- LeetCode 1008. Construct Binary Search Tree from Preorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 题目: Retu ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
随机推荐
- 【HNOI2014】画框
题面 题解 这又是一种套路啊233 将\(\sum a_i\)和\(\sum b_i\)分别看做\(x\)和\(y\),投射到平面直角坐标系中,于是就是找\(xy\)最小的点 于是可以先找出\(x\) ...
- browser-sync 文件监听失败的解决方案
问题 为了方便实时预览前端开发过程中修改源码后的页面,我在全球最大的同性交友网Github中找到了一个非常实用的工具,browser-sync. 安装使用方式请自行到官网https://browser ...
- Navigation - How to define the structure of the navigation tree via the NavigationItemAttribute
In the meantime, you should use the Model Editor to create such a navigation structure. There are se ...
- 使用Fiddler进行Web接口测试
一.Fiddler简介1.为什么是Fiddler?抓包工具有很多,小到最常用的web调试工具firebug,达到通用的强大的抓包工具wireshark.为什么使用fiddler?原因如下: A)Fir ...
- 宝塔中mysql数据库命名小坑
今天在通过宝塔新建网站,添加mysql数据库,名字中间有下划线,发现能够创建成功,但是实际链接后,是没有这个数据库的.是宝塔的原因还是liunx服务器的原因? 不支持下划线的数据库名字吗? 比如 bo ...
- jmeter线程组介绍
Jmeter中的测试计划是一直有的,但可以在右侧修改名字,要开始做具体测试设计前,都需要在测试计划下边添加一个线程组,添加路径为鼠标捕获测试计划后,点击鼠标右键->添加->Threads( ...
- hdfs命令大全
hdfs常用命令: 第一部分:hdfs文件系统命令 第一类:文件路径增删改查系列: hdfs dfs -mkdir dir 创建文件夹 hdfs dfs -rmr dir 删除文件夹dir hdf ...
- GitHub笔记(二)——远程仓库的操作
二 远程仓库 1 创建联系 第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一 ...
- Netty源码分析第2章(NioEventLoop)---->第1节: NioEventLoopGroup之创建线程执行器
Netty源码分析第二章: NioEventLoop 概述: 通过上一章的学习, 我们了解了Server启动的大致流程, 有很多组件与模块并没有细讲, 从这个章开始, 我们开始详细剖析netty的各个 ...
- Hyperledger Fabric MSP Identity Validity Rules——MSP身份验证规则
MSP Identity Validity Rules——MSP身份验证规则 正如Hyperledger Fabric Membership Service Providers (MSP)——成员服务 ...