Construct Binary Tree from Preorder and Inorder Traversal leetcode java
题目:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
题解:
/ \ / \ / \ 对于上图的树来说,
index: 0 1 2 3 4 5 6
先序遍历为: 2 4 5 3 6 7
中序遍历为: 4 2 5 1 6 3 7
为了清晰表示,我给节点上了颜色,红色是根节点,蓝色为左子树,绿色为右子树。
可以发现的规律是:
1. 先序遍历的从左数第一个为整棵树的根节点。
2. 中序遍历中根节点是左子树右子树的分割点。 再看这个树的左子树:
先序遍历为:
中序遍历为: 4 5
依然可以套用上面发现的规律。 右子树:
先序遍历为:
中序遍历为: 6 7
也是可以套用上面的规律的。 所以这道题可以用递归的方法解决。
具体解决方法是:
通过先序遍历找到第一个点作为根节点,在中序遍历中找到根节点并记录index。
因为中序遍历中根节点左边为左子树,所以可以记录左子树的长度并在先序遍历中依据这个长度找到左子树的区间,用同样方法可以找到右子树的区间。
递归的建立好左子树和右子树就好。 代码如下:
1 public TreeNode buildTree(int[] preorder, int[] inorder) {
2 int preLength = preorder.length;
3 int inLength = inorder.length;
4 return buildTree(preorder, 0, preLength-1, inorder, 0, inLength-1);
5 }
6
7 public TreeNode buildTree(int[] pre, int preStart, int preEnd, int[] in, int inStart, int inEnd){
8 if(inStart > inEnd || preStart > preEnd)
9 return null;
int rootVal = pre[preStart];
int rootIndex = 0;
for(int i = inStart; i <= inEnd; i++){
if(in[i] == rootVal){
rootIndex = i;
break;
}
}
int len = rootIndex - inStart;
TreeNode root = new TreeNode(rootVal);
root.left = buildTree(pre, preStart+1, preStart+len, in, inStart, rootIndex-1);
root.right = buildTree(pre, preStart+len+1, preEnd, in, rootIndex+1, inEnd);
return root;
}
Reference:http://edwardliwashu.blogspot.com/2013/01/construct-binary-tree-from-preorder-and.html
Construct Binary Tree from Preorder and Inorder Traversal leetcode java的更多相关文章
- Construct Binary Tree from Preorder and Inorder Traversal [LeetCode]
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——LeetCode
Given preorder and inorder traversal of a tree, construct the binary tree. 题目大意:给定一个二叉树的前序和中序序列,构建出这 ...
- 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: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 【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] 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 ...
随机推荐
- [Java]Spring框架
在这里学习Spring框架: >>spring&struts框架学习 >>spring >>Java回顾之Spring基础 >>IBM Java ...
- 使用httpclient需要的maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <dependency& ...
- j.u.c系列(10)---之并发工具类:Semaphore
写在前面 Semaphore是一个计数信号量,它的本质是一个"共享锁". 信号量维护了一个信号量许可集.线程可以通过调用acquire()来获取信号量的许可:当信号量中有可用的许可 ...
- MySQL_事务没有提交导致 锁等待 Lock wait timeout exceeded
java.lang.Exception:### Error updating database. Cause: java.sql.SQLException: Lock wait timeout ex ...
- 用Docker下搭建GitLab
最近试了一下Docker,发现用它搭建服务十分方便,就用它搭建了一个gitlab练练手. 首先下载gitlab镜像: docker image pull gitlab/gitlab-c ...
- CentOS 7.x,不重新编译 PHP,动态安装 imap 扩展
先前的教程:PHP5不重新编译,如何安装自带的未安装过的扩展,如soap扩展? # 安装依赖包 yum install -y libc-client-devel /usr/local/src/cent ...
- CF 427D Match & Catch 求最短唯一连续LCS
题目来源:CF 427D Match & Catch 题意:给出2个字符串 求最短的连续的公共字符串 而且该字符串在原串中仅仅出现一次 思路:把2个字符串合并起来求height 后缀数组hei ...
- [置顶] 九度笔记之 1494:Dota
题目1494:Dota 1 秒 内存限制:128 兆 特殊判题:否 提交:559 解决:122 题目描述: 大家都知道在dota游戏中,装备是对于英雄来说十分重要的要素. 英雄们不仅可以购买单个的装备 ...
- 魔兽私服TrinityCore 运行调试流程
配置参见上一篇:TrinityCore 魔兽世界私服11159 完整配置 (1)启动Web服务器 打开TC2_Web_Mysql目录,运行“启动Web服务器.exe” 自动弹出帐号注册界面,并启动Ap ...
- 一些 Google 搜索词
(1) flex blazeds java; (2) flex 动画 || flex animation || flex spark glow animation (3) flex glow效果 ...