【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树
(没思路,典型记住思路好做)
根据一棵树的前序遍历与中序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:
3
/ \
9 20
/ \
15 7
链接:https://www.nowcoder.com/questionTerminal/0ee054a8767c4a6c96ddab65e08688f4
来源:牛客网
/*
* 假设树的先序遍历是12453687,中序遍历是42516837。
* 这里最重要的一点就是先序遍历可以提供根的所在,
* 而根据中序遍历的性质知道根的所在就可以将序列分为左右子树。
* 比如上述例子,我们知道1是根,所以根据中序遍历的结果425是左子树,而6837就是右子树。
* 接下来根据切出来的左右子树的长度又可以在先序便利中确定左右子树对应的子序列
* (先序遍历也是先左子树后右子树)。
* 根据这个流程,左子树的先序遍历和中序遍历分别是245和425,
* 右子树的先序遍历和中序遍历则是3687和6837,我们重复以上方法,可以继续找到根和左右子树,
* 直到剩下一个元素。
*/
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTree(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
}
private TreeNode buildTree(int[] preorder, int preLeft, int preRight, int[] inorder, int inLeft, int inRight) {
if(preRight<preLeft)
return null;
TreeNode node=new TreeNode(preorder[preLeft]);
if(preRight==preLeft)
return node;
int num=0;
for(int i=inLeft;i<=inRight;i++){
if(inorder[i]==preorder[preLeft]){
num=i;
break;
}
}
int length=num-inLeft;
node.left=buildTree(preorder,preLeft+1,preLeft+length,inorder,inLeft,inLeft+length-1);
node.right=buildTree(preorder,preLeft+length+1,preRight,inorder,num+1,inRight);
return node;
}
看了答案自己写,边界条件老是搞错:
public class ConstructBinaryTree {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder.length <=0 || inorder.length<=0){
return null;
}
return helper(preorder,0,preorder.length-1,inorder,0,inorder.length-1); } public TreeNode helper(int[] preorder,int i,int j, int[] inorder,int m,int n) {
if (i>j) {
return null;
}
int root = preorder[i];
TreeNode node = new TreeNode(root);
int leftlength = 0;
for (int k=m;k<=n;k++) {
if (root == inorder[k]) {
leftlength = k-m;
}
}
node.left = helper(preorder, i+1, i+leftlength,inorder, m, m+leftlength-1);
node.right = helper(preorder, i+leftlength+1, j,inorder, m+leftlength+1, n);
return node;
}
}
106. 从中序与后序遍历序列构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:
3
/ \
9 20
/ \
15 7 与105从前序与中序遍历序列构造二叉树的区别就是后序序列最后一个是根节点,其他一样
【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树的更多相关文章
- leetcode 105 106 从前序与中序遍历序列构造二叉树 从中序与后序遍历序列构造二叉树
题目: 105 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = ...
- 已知树的前序、中序,求后序的java实现&已知树的后序、中序,求前序的java实现
public class Order { int findPosInInOrder(String str,String in,int position){ char c = str.charAt(po ...
- [Java]算术表达式组建二叉树,再由二叉树得到算式的后序和中序表达式
Entry类: package com.hy; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...
- Leetcode(105)-从前序与中序遍历序列构造二叉树
根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3,15, ...
- 已知树的前序、中序,求后序的c++实现&已知树的后序、中序,求前序的c++实现
#include"iostream" using namespace std; int pre[30]; int in[30]; int post[30]; int indexOf ...
- 【LeetCode】105 & 106. Construct Binary Tree from Inorder and Postorder Traversal
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
- Leetcode 106. 从中序与后序遍历序列构造二叉树
题目链接 https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/descri ...
- 树的三种DFS策略(前序、中序、后序)遍历
之前刷leetcode的时候,知道求排列组合都需要深度优先搜索(DFS), 那么前序.中序.后序遍历是什么鬼,一直傻傻的分不清楚.直到后来才知道,原来它们只是DFS的三种不同策略. N = Node( ...
随机推荐
- zabbix 监控 WEB 应用性能
1.介绍使用 zabbix_sender 发送采集的 WEB 状态值,使用 pycurl 来采集 WEB 状态zabbix_sender发送数据,需保证主机名与zabbix server记录的主机名一 ...
- How to intall and configure Haproxy on Centos
Install Haproxy CentOS/RHEL 5 , 32 bit:# rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-r ...
- 搜索引擎(Elasticsearch搜索详解)
学完本课题,你应达成如下目标: 掌握ES搜索API的规则.用法. 掌握各种查询用法 搜索API 搜索API 端点地址 GET /twitter/_search?q=user:kimchy GET /t ...
- Android 开发中 SQLite 数据库的使用
SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP, ...
- Nginx代理MysqlCluster集群
-------Nginx代理MysqlCluster 公司有一个公网ip,有公网ip(222.222.222.222)那台服务器上装的nginx,mysql装在公司另外一台服务器上假设ip为192.1 ...
- 状压DP总结
状态压缩就是将一行的状态压成一个二进制数,这个数的二进制形式反映了这一行的情况 比如0100111的意义为:这一排的第一个数没被使用,第二个被占用了,第三四个没被占用,第五六七个被占用 我们知道位运算 ...
- Ocean的游戏(前缀和)
题目链接:http://oj.ismdeep.com/contest/Problem?id=1284&pid=1 B: Ocean的游戏 Time Limit: 1 s Memory ...
- 任意模数NTT
任意模数\(NTT\) 众所周知,为了满足单位根的性质,\(NTT\)需要质数模数,而且需要能写成\(a2^{k} + r\)且\(2^k \ge n\) 比较常用的有\(998244353,1004 ...
- P3373 线段树模板
好,这是一个线段树模板. #include <cstdio> using namespace std; ; long long int sum[N],tag1[N],tag2[N],mo; ...
- A1143. Lowest Common Ancestor
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...