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:

  1. /**
  2. * Definition for binary tree
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public TreeNode buildTree(int[] preorder, int[] inorder) {
  12. TreeNode root = buildTreeRecur(preorder,inorder,0,preorder.length-1,0,inorder.length-1);
  13. return root;
  14. }
  15.  
  16. public TreeNode buildTreeRecur(int[] preorder, int[] inorder, int preS, int preE, int inS, int inE){
  17. if (preS>preE) return null;
  18. if (preS==preE){
  19. TreeNode leaf = new TreeNode(preorder[preS]);
  20. return leaf;
  21. }
  22.  
  23. int rootVal = preorder[preS];
  24. int index = inS;
  25. for (int i=inS;i<=inE;i++)
  26. if (inorder[i]==rootVal){
  27. index = i;
  28. break;
  29. }
  30.  
  31. int leftLen = index-inS;
  32. int rightLen = inE - index;
  33.  
  34. TreeNode leftChild = buildTreeRecur(preorder,inorder,preS+1,preS+leftLen,inS,index-1);
  35. TreeNode rightChild = buildTreeRecur(preorder,inorder,preE-rightLen+1,preE,index+1,inE);
  36. TreeNode root = new TreeNode(rootVal);
  37. root.left = leftChild;
  38. root.right= rightChild;
  39. return root;
  40. }
  41.  
  42. }

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. Bootstrap CSS 描述

    <!DOCTYPE html><html lang="zh-CN"><head> <!--定于内容,和内容的编码格式--> < ...

  2. 快速解决Canvas.toDataURL 图片跨域的问题

    出现Canvas.toDataURL 图片跨域问题怎么解决呢?下面小编就为大家带来一篇Canvas.toDataURL 图片跨域问题的快速解决方法.一起跟随小编过来看看吧 如题,在将页面的图片地址进行 ...

  3. jQuery中$.ajax()和$.getJson()同步处理详解

    一.前言 为什么需要用到同步,因为有时候我们给一个提交按钮注册提交表单数据的时候,在提交动作之前会进行一系列的异步ajax请求操作,但是页面js代码会按顺序从上往下面执行,如果你在这过程中进行了异步操 ...

  4. 转载:T-SQL语句大全

    一.基础 1.创建数据库 CREATE DATABASE database-name 2.删除数据库 drop database dbname 3.备份数据库 --- 创建 备份数据的 device ...

  5. .Net 内存泄露

    一.事件引起的内存泄露 1.不手动注销事件也不发生内存泄露的情况 我们经常会写EventHandler += AFunction; 如果没有手动注销这个Event handler类似:EventHan ...

  6. nodejs服务器anywhere简介

    一句话:随时随地将你的当前目录变成一个静态文件服务器的根目录. 安装 npm install anywhere -g 执行 $ anywhere // or with port $ anywhere ...

  7. php验证是否是md5编码的代码

    php验证是否是md5编码的示例. 代码很简单,使用了正则表达式. function is_md5($password) {     return preg_match("/^[a-z0-9 ...

  8. 一款点击图片进行无限循环的jquery手风琴特效

    一款点击图片进行无限循环的jquery手风琴特效,点击手风琴折合点,可以无限循环的点击下去,很炫酷的手风琴哟! 还有每张图片的文字介绍,因为兼容IE6所以找来分享给大家这个jquery特效. 适用浏览 ...

  9. Android工程目录及其作用简介

    1. src:存放所有的*.java源程序. 2. gen:为ADT插件自动生成的代码文件保存路径,里面的R.java将保存所有的资源ID. 3. assets:可以存放项目一些较大的资源文件,例如: ...

  10. 生产WCF客户端类文件的命令格式

    生产WCF客户端类文件的命令格式: svcutil.exe net.tcp://127.0.0.1:8732/ChromaMI.Remote.ConfigService/RemoteConfigSer ...