Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

Solution:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
TreeNode root = buildTreeRecur(preorder,inorder,0,preorder.length-1,0,inorder.length-1);
return root;
} public TreeNode buildTreeRecur(int[] preorder, int[] inorder, int preS, int preE, int inS, int inE){
if (preS>preE) return null;
if (preS==preE){
TreeNode leaf = new TreeNode(preorder[preS]);
return leaf;
} int rootVal = preorder[preS];
int index = inS;
for (int i=inS;i<=inE;i++)
if (inorder[i]==rootVal){
index = i;
break;
} int leftLen = index-inS;
int rightLen = inE - index; TreeNode leftChild = buildTreeRecur(preorder,inorder,preS+1,preS+leftLen,inS,index-1);
TreeNode rightChild = buildTreeRecur(preorder,inorder,preE-rightLen+1,preE,index+1,inE);
TreeNode root = new TreeNode(rootVal);
root.left = leftChild;
root.right= rightChild;
return root;
} }

Construct Binary Tree from Preorder and Inorder Traversal

Leetcode-Construct Binary Tree from inorder and preorder travesal的更多相关文章

  1. Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  2. [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树

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

  3. 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 ...

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

    /* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root ...

  5. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  6. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  7. [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

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

  8. [LeetCode] Construct Binary Tree from Inorder and Pretorder Traversal

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

  9. Leetcode Construct Binary Tree from Inorder and Postorder Traversal

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

随机推荐

  1. Install SQLite

    http://www.tutorialspoint.com/sqlite/sqlite_installation.htm The SQLite is famous for its great feat ...

  2. js实现文字截断

    先前用jq做了一个文字截断功能,但是不用jq的项目要实现此功能还要引如jq显得过于麻烦.这里写了一个js的文字截断功能.直接上代码. HTML(测试用的): <div>我是pox我是pox ...

  3. ASP.NET中的状态保持(转载)

    状态是某一类型的数据在一定时期内保持活跃的信息.这里说的一定时期可以使整个应用程序的生命周期,可以使用户操作程序的时间,当然也可以是单个页面的生命周期等.  为了解决传统Web编程中固有的限制,ASP ...

  4. [leetcode]_String to Integer (atoi)

    非常考虑思维全面性的一道题,考验是否能够考虑本问题的方方面面. 题目:将一个string转换为int.实现函数atoi()的功能. 先应该明确atoi()有哪些特殊功能:(正常的正负数情况我就不列了) ...

  5. html5面向对象做一个贪吃蛇小游戏

    canvas加面向对象方式的贪吃蛇 2016-08-25 这个小游戏可以增加对面向对象的理解,可以加强js逻辑能力,总之认真自己敲一两遍收获还是不少啊!!适合刚学canvas的同学练习!! 废话不多说 ...

  6. Sublime Text2 注册码 汉化 配置lua开发环境

    1.注册  help --> Enter License ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-8556 ...

  7. Pycharm 使用 (一)

    学习[Python基础教程]到后面的练习阶段就觉得python自带的IDLE有点out的感觉,于是就在网上搜索好用的IDE, 挺多人推荐Pycharm的 不仅跨平台而且还支持django等框架; 初次 ...

  8. Sublime Text 2编译python时出错

    [Error 2] The system cannot find the file specified [Finished]   解决方法: 1.环境变量path添加: C:\Python32\Too ...

  9. GNU make 规则

    clean : rm *.tmp 规则格式: targets : prerequisites recipe ... targets : prerequisites : recipe recipe .. ...

  10. C/C++ 对常见字符串库函数的实现

    在c中的string.h头文件中存在很多对字符串进行操作的函数,利用这些函数可以方便的对字符串进行操作.下面将对常见的字符串函数进行解释和实现. strcpy 函数原型:char* _strcpy(c ...