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. Find out who the “mole” is?

    Blueheat Company’s  production server was out of order again. The CEO was very upset and want their ...

  2. WeChat 6.3 wipe deleted chat messages as well as LINE 5.3 and above

    Let me show you the WeChat version first. It is 6.3. What will happen to WeChat deleted chat message ...

  3. JavaScript编程规范

    最近看NodeJS中,有一部分写JS约定俗成的编程规范(附录B,详情参考附件),感觉在实际工作中能用到, 大致意思分享给大家,详情参考附件: 1.缩进:建议两空格 作为Node.js代码的缩进标记: ...

  4. C puzzles详解

    题目:http://www.gowrikumar.com/c/ 参考:http://wangcong.org/blog/archives/291 http://www.cppblog.com/smag ...

  5. Tomcat启动过程(三):从SocketProcessor到Container

    1.Http11Protocol中的内部类Http11ConnectionHandler,执行其process方法 if (processor == null) { processor = creat ...

  6. iOS初学者的AppStore上架应用"菜谱大师"开源了!

    本人是一名DoNet程序猿,在业余的时间自学了点iOS,于是就自己弄了一个小菜谱,自己要做菜的时候也就可以用自己的菜谱了. 现在将此应用开源给像我一样对iOS开发有兴趣,并且想学习iOS的园友,毕竟这 ...

  7. [Redis] RDB & AOF

    http://my.oschina.net/davehe/blog/174662 rdb - 存在dump.rdb 的二进制文件中 dump 整个db, 数据多的时候,不合适频繁保存,保存的时间间隔应 ...

  8. spark streaming kafka1.4.1中的低阶api createDirectStream使用总结

    转载:http://blog.csdn.net/ligt0610/article/details/47311771 由于目前每天需要从kafka中消费20亿条左右的消息,集群压力有点大,会导致job不 ...

  9. PHP-Fcgi下PHP的执行时间设置方法

    昨天,一个程序需要导出500条数据,结果发现到150条是,Nginx报出504 Gateway Timeout错误,原来PHP-Fcgi下的设置执行时间与isapi的不同     一般情况下设置PHP ...

  10. 西门子SIMATIC IT平台

    西门子公司的SIMATIC IT平台基于ANSI/ISA S95标准开发,包含的功能组件覆盖了ISA S95规定的生产业务操作模型,同时也满足MESA所确定的MES系统11项功能要求. SIMATIC ...