【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 ...
随机推荐
- 【代码笔记】Web-JavaScript-JavaScript 变量
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 纯小白入手 vue3.0 CLI - 2.7 - 组件之间的数据流
vue3.0 CLI 真小白一步一步入手全教程系列:https://www.cnblogs.com/ndos/category/1295752.html 尽量把纷繁的知识,肢解重组成为可以堆砌的知识. ...
- DNS协议总结
1.DNS用于根据域名返回ip地址. 2.一般情况下,DNS-server是通过在UDP协议与客户端之间交互的,UDP端口号是53. 特别注意.DNS有时会使用TCP 53端口与客户端进行交互,所以, ...
- Linux 操作系统下的环境变量设置
Linux下的环境变量设置 by:授客 QQ:1033553122 1. 问题描述 linux输入命令时经常会出现提示:xxx:Command not found 2. 原因分析 Command ...
- Linux 学习笔记之超详细基础linux命令 Part 13
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 12---------------- ...
- git批量修改已经提交的commit的姓名和邮箱
首先,我们创建change.sh脚本,并根据个人信息复制以下脚本. #!/bin/sh git filter-branch --env-filter ' OLD_EMAIL="填写原来的邮箱 ...
- HDMI驱动热插拔检测方法
1. 使用poll机制 1.1 如何使用? a. open("/dev/HPD"); b. poll状态发生变化 c. read确定接上还是接下 1.2 情景分析: APP使用op ...
- js 提交表单添加csrf
function post(path, shipmentMap, method) { method = method || "post"; // Set method to pos ...
- 一个CSS值转REM的Sublime Text插件
CSSREM 一个CSS的px值转rem值的Sublime Text 3自动完成插件. 插件效果如下: 安装 下载本项目,比如:git clone https://github.com/flashli ...
- web笔试
类型判断用到哪些方法? typeof和instanceof 值类型和引用类型的区别? 根据 JavaScript中的变量类型传递方式,又分为值类型和引用类型,在参数传递方式上,值类型是按值传递,引用类 ...