【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★
1.题目

2.思路


3.java代码
//测试
public class BuildTreeUsingInorderAndPreorder {
public static void main(String[] args) {
int[] preSort={1,2,4,7,3,5,6,8};
int[] inSort={4,7,2,1,5,3,8,6};
System.out.println(new Solution().buildTree(preSort, inSort));
}
}
//利用前序和中序重建二叉树
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
//参数校验
if(preorder==null||inorder==null||preorder.length!=inorder.length||preorder.length==0)
return null;
return buildTreeCore(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
}
/**
* 构建二叉树,数据输入的正确性由输入数据自己保证
*
* @param preorder 先序遍历的结果
* @param startPreorder 先序遍历的开始位置
* @param endPreorder 先序遍历的结束位置
* @param inorder 中序遍历的结果
* @param startInorder 中序遍历的开始位置
* @param endInorder 中序遍历的结束位置
* @return 二叉树的根结点
*/
private TreeNode buildTreeCore(int[] preorder, int startPreorder, int endPreorder,
int[] inorder,int startInorder, int endInorder) {
// 只有一个元素时直接返回该节点,这也是递归结束的出口标志
if(startPreorder==endPreorder){
return new TreeNode(preorder[startPreorder]);
}else{
// 记录根结点的在中序遍历中的位置
int rootIn=startInorder;
for(int i=startInorder;i<=endInorder;i++){
if(inorder[i]==preorder[startPreorder]){
rootIn=i;
break;
}
}
// 创建根结点
TreeNode root=new TreeNode(inorder[rootIn]);
// 左子树的结点个数
int leftLength=rootIn-startInorder;
if(leftLength>0){
// startPreorder+1, startPreorder+leftLength:左子树在前序序列中的起始和结束位置
root.left=buildTreeCore(preorder, startPreorder+1, startPreorder+leftLength, inorder, startInorder, rootIn-1);
}
// 右子树的结点个数
int rightLength=endInorder-rootIn;
if(rightLength>0){
// startPreorder+leftLength+1, endPreorder:左子树在前序序列中的起始和结束位置
root.right=buildTreeCore(preorder, startPreorder+leftLength+1, endPreorder, inorder, rootIn+1, endInorder);
}
return root;
}
}
}
//二叉树节点定义
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★的更多相关文章
- 【树】Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后 ...
- 【Leetcode】【Medium】Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
- 【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- leetcode105:Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- 【题解二连发】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 - LeetCode Construct Binary ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 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 ...
随机推荐
- 【读书笔记】iOS-访问网络
iOS平台是按照一直有网络连接的思路来设计的,开发者利用这一特点创造了很多优秀的第三方应用.大多数的iOS应用都需要联网,甚至有些应用严重依赖网络,没有网络就无法正常工作. "在访问网络失败 ...
- 理解jQuery中$.get、$.post、$.getJSON和$.ajax的用法
ajax的4种方法:$.get.$.post.$getJSON.$ajax. 1.$.get $.get()方法使用GET方式来进行异步请求,它的语法结构为: $.get( url [, data] ...
- C# winform三种定时方法
1. 直接用winform 的 timers 拖控件进去 代码 public partial class Form1 : Form { public Form1() ...
- shell下变量比较最佳实践
https://stackoverflow.com/questions/13617843/unary-operator-expected If you know you're always going ...
- C#语言————拼接、插入、替换、删除四种方法
StringBuilder sb = new StringBuilder("hello"); sb.Append("world");//拼接 sb.Insert ...
- Nginx Linux安装与部署
Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行. ...
- ccf-20161203--权限查询
这题我的思路是将用户直接与他的权限联系起来.比如: 用户 角色 权限 Alice hr crm:2直接转变为:Alice: crm:2 题目与代码如下: 问题描述 试题编号: 201612-3 试题名 ...
- Orcale日期函数to_date(),to_char()
日期转换的两个函数分别是to_date()和to_char(),to_date() 作用将字符类型按一定格式转化为日期类型, to_char() 将日期转按一定格式换成字符类型 其中当时间需要精确的时 ...
- Java引用类型转换
java的引用类型转换分为两种: 向上类型转换,是小类型到大类型的转换 向下类型转换,是大类型到小类型的转换 现存在一个Animal动物类,猫子类和狗子类继承于Animal父类: 1 public c ...
- 【转载】Linux 内存管理机制
在Linux中经常发现空闲内存很少,似乎所有的内存都被系统占用了,表面感觉是内存不够用了,其实不然.这是Linux内存管理的一个优秀特性,主要特点是,无论物理内存有多大,Linux 都将其充份利用,将 ...