问题重述:

问题求解:

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

对于上图的树来说,
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. Github上600多个iOS开源项目地址

    将Github上600多个iOS开源项目进行分类并且有相应介绍,小伙伴们快来看呀 地址:http://github.ibireme.com/github/list/ios/

  2. mysql 12小时和24小时转换

    1.12小时显示 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String ...

  3. C# 中重载自增自减操作符的具体运算原理 ----从C++程序员的角度看C#自增操作符重载的实质

    看了看C#的运算符重载,发现与C++打不相同.刚刚被C#的自增操作符坑了,现在来分享一下. 先定义一个类 class A { public int i; public A(int I) { i = I ...

  4. JDK/bin下工具列表说明

    JDK/bin下工具列表说明 appletviewer.exe:一种执行HTML文件上的Java小程序类的Java浏览器 apt.exe:注解处理工具(Annotation Processing To ...

  5. 【Leetcode】【Medium】3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  6. configure编译,显示错误不合法的选项空

    解决: 注释的问题,讲注释重启起一行,或者取消掉就可以了 不要在反斜杠后面在写任何东西,他会把你写的东西当作新的一行的输入内容的

  7. Mysql学习---基础操作学习

    1.1. 基本操作 数据库引擎 Inodb:支持事务[原子性操作,完成一些列操作后才算完成操作,否则rollback] MyISAM: 支持全文索引,强调了快速读取操作,主要用于高负载的select ...

  8. IE 浏览器中英文切换

    Oracle相关产品浏览器中可以调节显示中英文

  9. HBuilde H5开发,关于JSON的Storage存储

    今天踩坑了,在这里记一下. 我想做一个列表,开始是一个一个复制粘贴,然后发现这样太不灵活了,如果我有更多内容要填难道还要再一个一个复制吗? 所以我想到直接用JS动态生成最好,我的思路是这样的: //首 ...

  10. 360网站卫士SQL注入绕过案例一个

    不要以为用了360就可以高枕无忧,直接在netcraft的site_report中找到源站服务器IP,直接SQL脱裤,甚至可获取服务器权限. 存在漏洞的网站: 手工测试存在注入点: 但是网站有360保 ...