Leetcode-Construct Binary Tree from inorder and preorder travesal
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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 105. Construct Binary Tree from Inorder and preorder Traversal
/* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- SQL2005中使用identity_insert向自动增量字段中写入内
摘自: http://www.aspbc.com/tech/showtech.asp?id=1117 SQL2005以前的数据库是不允许向自动增量字段中写入内容的,ACCESS也不行,但在SQL200 ...
- 简单linux块设备驱动程序
本文代码参考<LINUX设备驱动程序>第十六章 块设备驱动程序 本文中的“块设备”是一段大小为PAGE_SIZE的内存空间(两个扇区,每个扇区512字节) 功能:向块设备中输入内容,从块设 ...
- 基于opencv网络摄像头在ubuntu下的视频获取
基于opencv网络摄像头在ubuntu下的视频获取 1 工具 原料 平台 :UBUNTU12.04 安装库 Opencv-2.3 2 安装编译运行步骤 安装编译opencv-2.3 参 ...
- vim插件之SnipMate
SnipMate简介 snipMate一款功能强大的代码补齐插件,可自定义代码模板,并具备单词补齐的功能. vim插件snipMate下载地址 SnipMate安装 将snipMate.zip解压到~ ...
- Vue.js学习 Item13 – 指令系统与自定义指令
基础 除了内置指令,Vue.js 也允许注册自定义指令.自定义指令提供一种机制将数据的变化映射为 DOM 行为. 可以用 Vue.directive(id, definition) 方法注册一个全局自 ...
- PHP mysql 事务处理实例
事务是必须满足4个条件(ACID):原子性(Autmic).一致性(Consistency).隔离性(Isolation).持久性(Durability) 原子性(Autmic):事务在执行性,要做到 ...
- Android开发之Source无法覆写public void onClick(View v)
初学Android开发,在为一个按钮[该按钮继承OnClickListener()]写监听时,发现无法在Source中引入public void onClick(View v),当时非常纳闷,平常情况 ...
- DELPHI XE5 FOR ANDROID 模仿驾考宝典 TMEMO 控件随着字数增多自动增高
在一个安卓需求中,需要模仿驾考宝典的详解部分.琢磨了好几天.终于搞定: MemoAns.Height:=10;//MEMO控件赋初始高度值 MemoAns.Lines.Clear; MemoAns.W ...
- How to get the underlying SSRS Report Query, reset query , add your own ranges and execute report [AX2012]
Below is the small code snippet to get the underlying query of the SSRS report, reset query, prompt ...
- Generate List and Table via ng-repeat
<div ng-app ng-controller='StudentListController'> <ul> <li ng-repeat='student in stu ...