题目链接

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

法一: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. 关于 [lambda x: x*i for i in range(4)] 理解

    题目: lst = [lambda x: x*i for i in range(4)] res = [m(2) for m in lst] print res 实际输出:[6, 6, 6, 6] 想要 ...

  2. vscode Variables Reference

    vscode Variables Reference 您可以在以下链接中找到该列表:https://code.visualstudio.com/docs/editor/variables-refere ...

  3. 注解失效,@SpringBootApplication 失效,引入包失效

    因为同时修改两个springboot工程,其中把一个工程的版本调整到2.0.2.RELEASE,然后坑爹的事情出现了,所有springboot工程的@SpringBootApplication失效, ...

  4. [国家集训队]最长双回文串 manacher

    ---题面--- 题解: 首先有一个直观的想法,如果我们可以求出对于位置i的最长后缀回文串和最长前缀回文串,那么我们枚举分界点然后合并前缀和后缀不就可以得到答案了么? 所以我们的目标就是求出这两个数列 ...

  5. HDOJ.2111 Saving HDU (贪心)

    Saving HDU 点我挑战题目 题意分析 给出来背包容量v和物品数量n,接下来n行分别给出每个商品单位体积的价值和物品总共的体积(注意是单位体积,不是每个物品).求出最多能装多少价值的物品. 典型 ...

  6. 【贪心】【CF3D】 Least Cost Bracket Sequence

    传送门 Description 给一个序列,序列里面会有左括号.问号.右括号.对于一个\(?\)而言,可以将其替换为一个\((\),也可以替换成一个\()\),但是都有相应的代价.问:如何替换使得代价 ...

  7. 史上最全Linux提权后获取敏感信息方法

    http://www.freebuf.com/articles/system/23993.html 在本文开始之前,我想指出我不是专家.据我所知,在这个庞大的区域,没有一个“神奇”的答案.分享,共享( ...

  8. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集

    A. Petr and a calendar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. HDU1254 bfs

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  10. 2015/8/28 Python基础(2):对象

    Python用对象模型来存储数据.构造任何类型的值都是一个对象.Python对象都有是三个特性:身份,类型和值 身份是每个对象的唯一身份标识.任何对象都可以用内建函数id()来得到身份.如: > ...