题目链接

题目大意:根据先序遍历和中序遍历构造二叉树。

法一:DFS。根据模拟步骤,直接从先序和中序数组中找值然后加入二叉树中,即先从先序数组中确定根结点,然后再去中序数组中确定左子树和右子树的长度,然后根据左子树和右子树的长度,去划分先序数组和中序数组,确定左子树和右子树。代码如下(耗时15ms):

     public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length == 0 || inorder.length == 0) {
return null;
}
return dfs(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1);
}
//preL是当前先序数组的第一个结点下标,preR是当前先序数组的最后一个结点下标
//inL是当前后序数组的第一个结点下标,inR是当前后序数组的最后一个结点下标
private TreeNode dfs(int[] preorder, int[] inorder, int preL, int preR, int inL, int inR) {
//将当前先序数组的第一个结点加入二叉树中,这个结点其实就是当前子树的根节点
TreeNode root = new TreeNode(preorder[preL]);
//根据这个根节点,去中序数组中找到位置下标
int rootIndex = inL;
while(inorder[rootIndex] != preorder[preL]) {
rootIndex++;
}
//左子树长度,根据当前中序数组和刚才确定的根节点下标,计算左子树长度,即中序数组中根节点前面的则是左子树
int leftLen = rootIndex - inL;
//右子树长度,根据当前中序数组和刚才确定的根节点下标,计算右子树长度,即中序数组中根节点后面的则是右子树
int rightLen = inR - rootIndex;
if(leftLen != 0) {
//确定左子树
root.left = dfs(preorder, inorder, preL + 1, preL + leftLen, inL, inL + leftLen - 1);
}
else {
root.left = null;
}
if(rightLen != 0) {
//确定右子树
root.right = dfs(preorder, inorder, preR - rightLen + 1, preR, inR - rightLen + 1, inR);
}
else {
root.right = null;
}
return root;
}

105.Construct Binary Tree from Preorder and Inorder Traversal---《剑指offer》面试6的更多相关文章

  1. 【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 ...

  2. [LeetCode] 105. 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 ...

  3. leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  4. 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. ============== 基本功: 利用前序和 ...

  5. LeetCode OJ 105. 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 ...

  6. 【leetocde】 105. 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 ...

  7. LeetCode 105. 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 ...

  8. 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  9. (二叉树 递归) leetcode 105. 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 ...

随机推荐

  1. css边框以及其他常用样式

    1. 边框是1像素,实体的,红色的. <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  2. bzoj 3275: Number (最小割)

    题目的意思是要选一些数,但是这些数如果满足两个条件的话就不能一起被选. type arr=record toward,next,cap:longint; end; const maxn=; maxm= ...

  3. 蒟蒻Orion还要学的东西!

    这个ID多元化真是个麻烦的事情...... 一会KamijouIndex一会dedicatus545一会Orion的,乱死了啊啊啊啊 数据结构 圆方树 ETT 仙人掌 可持久化树套树 数学 洲阁筛 m ...

  4. [JSOI2008]小店购物 & bzoj4349:最小树形图 最小树形图

    ---题面(洛谷)--- ---题面(bzoj)--- 其实是同一道题,,,样例都一模一样 题解: 一开始看想了好久,,,还想到了最短路和最小生成树,,然而写的时候才意识到最小生成树应该要用无向边 其 ...

  5. 虚拟机网络连接模式中桥接模式和NAT模式的区别

    1.桥接模式:当虚拟机系统的网络连接模式为桥接模式时,相当于在主机系统和虚拟机系统之间连接了一个网桥,而网桥两端的网络都属于同一网络,主机和虚拟机是处于同一网络中的对等主机. 实例,在使用Xshell ...

  6. BZOJ3437 小P的牧场 【斜率优化dp】

    3437: 小P的牧场 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 1502  Solved: 836 [Submit][Status][Disc ...

  7. UVA.679 Dropping Balls (二叉树 思维题)

    UVA.679 Dropping Balls (二叉树 思维题) 题意分析 给出深度为D的完全二叉树,按照以下规则,求第I个小球下落在那个叶子节点. 1. 默认所有节点的开关均处于关闭状态. 2. 若 ...

  8. 2 Advanced Read/Write Splitting with PHP’s MySQLnd

    原文地址需FQ才能看  https://blog.engineyard.com/2014/advanced-read-write-splitting-with-phps-mysqlnd In part ...

  9. IT英语累积

    JPA: Java Persistence API   一种持久化规范 Spring Data:一种用于简化数据库访问,支持云服务的开源框架 Spring Data JPA:是Spring Data的 ...

  10. IDEA的使用总结篇-1

    随笔:随着回首所在的公司的日益扩大,所在的技术中心也日渐兵强马壮,但由于各位新老同仁的开发工具一直未曾统一,所以小编的老大终于一声令下,统一开发工具!统一使用IDEA,而且每个人今天要交一份IDEA的 ...