问题重述:

问题求解:

我们换一组比较有代表性的样例,

对于上图的树来说,
index: 0 1 2 3 4 5 6
先序遍历为: 2 4 5 3 6 7
中序遍历为: 4 2 5 6 3 7
为了清晰表示,我给节点上了颜色,红色是根节点,蓝色为左子树,绿色为右子树。
提取期中根节点的左子树 2 4 5,可以把2 4 5看作新的index,由此:
index: 2 4 5
先序遍历为:
中序遍历为: 5
同理,右子树也是如此。
这样不难看出本题应该用递归方法解决。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
TreeNode root = null;
if(preorder.length == 0) return root;
root = new TreeNode(preorder[0]);
if(preorder.length == 1 && inorder.length == 1) {
//root.val = preorder[0]; return root;
}
//root.val = preorder[0];
int flag = 0;
for(int i=0;i<inorder.length;i++){
if(inorder[i] == preorder[0]) {
flag = i;
break;
}
}
TreeNode lNode = null;
TreeNode rNode = null;
root.left = buildTree(Arrays.copyOfRange(preorder,1,flag+1),Arrays.copyOfRange(inorder,0,flag));
root.right = buildTree(Arrays.copyOfRange(preorder,flag+1,preorder.length),Arrays.copyOfRange(inorder,flag+1,inorder.length));
return root;
}
}
												

LeetCodee 105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  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. js如何判断字符串里面是否含有某个字符串

    方法一: indexOf() (推荐) var str = "123"; console.log(str.indexOf("3") != -1 ); // tr ...

  2. IntelliJ IDEA开发工具println报错的解决方法

    IntelliJ IDEA 编译 JSP,出现 out.println 报错,下图所示: 报错原因:println报红,这是因为没有关联好服务器! 解决方案:点击File->Project st ...

  3. ERP与电子商务的集成

    目前现状: 一般来说,企业中存在三种流:物资流.资金流和信息流,其中,信息流不是孤立存在的,它与物资流和资金流密切相关,反映了物资和资金流动前.流动中和流动后的状况. 电子商务与ERP被分裂开来,没有 ...

  4. 【Machine Learning】分类与回归 区别

    一.分类与回归的区别 两类监督学习 Classification Regression 分类和回归的区别在于输出变量的类型(而非输入变量). 定性输出称为分类,或者说是离散变量预测(discrete) ...

  5. Qt 之 QSS(样式表语法)

    https://blog.csdn.net/liang19890820/article/details/51691212 简述 Qt样式表(以下统称QSS)的术语和语法规则几乎和CSS相同.如果你熟悉 ...

  6. 《effective c++》读书笔记(上)

    最近在读<Effective C++>,确实是经典之作,但是有的条款也需要一些细节补充,所以都列在这篇文章里,希望能不断更新,个人阅读的是第三版,不包括C++ 11的内容. 条款1:视C+ ...

  7. SQL Server ->> XML方法

    1. 得到XML类型中某个节点下子节点的数量 DECLARE @xml xml SET @xml = ' <Parameters> <Parameter name = "p ...

  8. Ten C++11 Features Every C++ Developer Should Use

    原版:http://www.codeproject.com/Articles/570638/Ten-Cplusplus-Features-Every-Cplusplus-Developer 译版:ht ...

  9. Python学习---Python下[字符串]的学习

    字符串[不可变] 重点方法: in count() center(50,'*') startswith('h') endwith('h') find('r') : 找不到返回 -1 index('r' ...

  10. 使用redis存放 map数据

    效果 实现 public class JedisPoolUtil { /** * 获取一个redis实例 * @param jedisConnectionFactory * @return */ pu ...