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 ...
随机推荐
- Unieap3.5-Grid翻页不提示修改
<toolbar export="{defaultType:'server'}" paging="{onPagingModified:ssSettleCheck.o ...
- C# 集合 — Hashtable 线程安全
基础知识重要吗?真的很重要. 就在笔者与同事聊天中突然同事提出一个问题,让笔都有点乱了手脚(有点夸张),题目是这样的: 问:Hashtable 是线程安全的吗? 答:…… (沉默中,Yes Or No ...
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(九)-- 单元测试
本篇将结合这个系列的例子的基础上演示在Asp.Net Core里如何使用XUnit结合Moq进行单元测试,同时对整个项目进行集成测试. 第一部分.XUnit 修改 Project.json 文件内容, ...
- Apache开启Proxy代理,实现域名端口转发
今天帮客户迁移网站,客户一个是ASPX的一个是PHP的网站,这时候有2个域名,可是php网站是Apache下的伪静态,必须要用到Apache,但是ASPX网站还必要到IIS+Mssql 然后到了这个时 ...
- 为什么使用 Redis及其产品定位 (转载自http://www.infoq.com/cn/articles/tq-why-choose-redis)
传统MySQL+Memcached架构遇到的问题 实际MySQL 是适合进行海量存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都曾经使用过这样的架构,但随着业务数据量的不 ...
- php 安装pdo_mysql 扩展
如果pdo在linux下出现exception 'PDOException' with message 'could not find driver'则问题是php编译时少加--with-pdo-my ...
- 在xml中添加array
在values建立arrays(名字可自定义)的xml: <?xml version="1.0" encoding="utf-8"?> <re ...
- andoroid项目使用Javah找不到class问题
比如目录结构是:Soffice\bin\classes\cn\com\isoffice\util\SofficeWebService.class 进入到bin/classes 下使用命令 javah ...
- 解决vmware安装 win7 后 没有虚拟网卡驱动 不能上网的问题
项目需要用到win7 32位系统,于是装个虚拟机,换了好几个系统资源,都是没有网卡驱动, XP 2003 都能上网唯独WIN7 不行,安装vmware tools也不管用,终于找到了这个东西.vmwa ...
- Hive启动时的棘手问题的处理
Hive是存在于Hadoop集群之上的数据仓库,作为大数据处理时的主要工具,对于大数据开发人员的重要性不言而喻.当然要使用Hive仓库的前提就是对于hive的安装,hive的安装是很简单的过程,主要关 ...