【题解二连发】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 Tree from Preorder and Postorder Traversal - LeetCode
题目大意
- 给定一棵二叉树的中序遍历和后序遍历,求这棵二叉树的结构。
- 给定一棵二叉树的前序遍历和中序遍历,求这棵二叉树的结构。
样例
Input: inorder = [9, 3, 15, 20, 7], postorder = [9, 15, 7, 20, 3]
Output: [3, 9, 20, null, null, 15, 7]
Input: preorder = [3, 9, 20, 15, 7], postorder = [9, 3, 15, 20, 7]
Output: [3, 9, 20, null, null, 15, 7]
解题思路
这两题的解题思路类似,主要应用了二叉树的这样一个结论:
对于任意一棵二叉树:
- 其前序遍历序列的第一个元素为该树的根
- 其后序遍历序列的最后一个元素为该树的根
然后对于一棵二叉树的遍历序列,其元素排布总是遵循如下规律:
- 前序遍历:
[根元素, [左子树元素], [右子树元素]] - 中序遍历:
[[左子树元素], 根元素, [右子树元素]] - 后序遍历:
[[左子树元素], [右子树元素], 根元素]
有了以上结论,这两题的思路就很明确了:先从前序(或后序)遍历中找到根元素,然后将遍历结果按照上面的元素分布规律分成三个部分,对于左子树和右子树,递归地调用该算法去构建,即可得出完整的结构。
根据以上思路,可以写出对数组进行分割的代码,记录如下:
前序遍历:
/// <summary>
/// 把二叉树的前序遍历序列分拆成左右两部分
/// 左右两部分中,若有一个部分不存在,则返回长度为 0 的数组
///
/// 在调用该方法前,需要提前为左右两部分的数组分配空间(即需要左右子树的序列大小)
/// </summary>
/// <param name="sourceArray">一个数组,表示二叉树的前序遍历序列</param>
/// <param name="leftPart">一个数组,表示这棵树的左子树的前序遍历序列</param>
/// <param name="rightPart">一个数组,表示这棵树的右子树的前序遍历序列</param>
private void SplitArray(int[] sourceArray, int[] leftPart, int[] rightPart)
{
// 对于前序遍历,各部分对应的下标范围为:
// - [0, 1):根节点
// - [1, 1 + leftPart.Length):左子树的前序遍历序列
// - [1 + leftPart.Length, sourceArray.Length):右子树的遍历序列 // 复制左子树内容
Array.Copy(sourceArray, 1, leftPart, 0, leftPart.Length);
// 复制右子树内容
Array.Copy(sourceArray, 1 + leftPart.Length, rightPart, 0, rightPart.Length);
}
中序遍历:
/// <summary>
/// 把二叉树的中序遍历序列分拆成左右两部分
/// 左右两部分中,若有一个部分不存在,则返回长度为 0 的数组
/// </summary>
/// <param name="sourceArray">一个数组,表示二叉树的中序遍历序列</param>
/// <param name="splitIndex">这棵二叉树的根节点,在中序遍历序列中的下标</param>
/// <param name="leftPart">输出参数,表示这棵树的左子树的中序遍历序列</param>
/// <param name="rightPart">输出参数,表示这棵树的右子树的中序遍历序列</param>
void SplitArray(int[] sourceArray, int splitIndex, out int[] leftPart, out int[] rightPart)
{
// 为左右两部分分配空间
// 对于中序遍历,各部分对应的下标范围为
// - [0, splitIndex):左子树的中序遍历序列
// - [splitIndex, splitIndex + 1):根节点
// - [splitIndex + 1, sourceArray.Length):右子树的中序遍历序列
leftPart = new int[splitIndex];
rightPart = new int[sourceArray.Length - (splitIndex + 1)]; // 复制左子树内容
Array.Copy(sourceArray, leftPart, leftPart.Length); // 复制右子树内容
Array.Copy(sourceArray, splitIndex + 1, rightPart, 0, rightPart.Length);
}
后序遍历:
/// <summary>
/// 把二叉树的后序遍历序列分拆成左右两部分
/// 左右两部分中,若有一个部分不存在,则返回长度为 0 的数组
///
/// 在调用该方法前,需要提前为左右两部分的数组分配空间(即需要左右子树的序列大小)
/// </summary>
/// <param name="sourceArray">一个数组,表示二叉树的后序遍历序列</param>
/// <param name="leftPart">一个数组,表示这棵树的左子树的后序遍历序列</param>
/// <param name="rightPart">一个数组,表示这棵树的右子树的后序遍历序列</param>
void SplitArray(int[] sourceArray, int[] leftPart, int[] rightPart)
{
// 对于后序遍历,各部分对应的下标范围为:
// - [0, leftPart.Length):左子树的后序遍历
// - [leftPart.Length, leftPart.Length + rightPart.Length):右子树的后序遍历
// - [leftPart.Length + rightPart.Length, sourceArray.Length):根节点 // 复制左子树内容
Array.Copy(sourceArray, leftPart, leftPart.Length);
// 复制右子树内容
Array.Copy(sourceArray, leftPart.Length, rightPart, 0, rightPart.Length);
}
Solution
Construct Binary Tree from Inorder and Postorder Traversal
/// <summary>
/// 根据二叉树的中序遍历序列和后序遍历序列,构建这棵二叉树
/// </summary>
/// <param name="inorder">一个数组,表示二叉树的中序遍历序列</param>
/// <param name="postorder">一个数组,表示二叉树的后序遍历序列</param>
/// <returns>构建出的二叉树的根节点</returns>
public TreeNode BuildTree(int[] inorder, int[] postorder)
{
// 递归终止条件:序列的长度为 0,返回 null
if (inorder.Length == 0 || postorder.Length == 0)
return null;
// 从后序遍历序列中找到根节点的值
int rootVal = postorder.Last();
// 在中序遍历序列中找到根节点对应的下标,以便分出左右部分
int rootIndex = Array.IndexOf(inorder, rootVal); // 提前为后序遍历的两部分分配内存空间
int[] postorderLeft = new int[rootIndex - 0];
int[] postorderRight = new int[inorder.Length - (rootIndex + 1)]; // 建立根节点
TreeNode root = new TreeNode(rootVal); // 拆分中序遍历序列
SplitArray(inorder, rootIndex, out int[] inorderLeft, out int[] inorderRight); // 拆分后序遍历序列
SplitArray(postorder, postorderLeft, postorderRight); // 递归地调用该方法以构建左右子树
root.left = BuildTree(inorderLeft, postorderLeft);
root.right = BuildTree(inorderRight, postorderRight); return root;
}
Construct Binary Tree from Preorder and Inorder Traversal
/// <summary>
/// 根据二叉树的中序遍历序列和前序遍历序列,构建这棵二叉树
/// </summary>
/// <param name="preorder">一个数组,表示二叉树的前序遍历序列</param>
/// <param name="inorder">一个数组,表示二叉树的中序遍历序列</param>
/// <returns>构建出的二叉树的根节点</returns>
public TreeNode BuildTree(int[] preorder, int[] inorder)
{
// 递归终止条件:序列的长度为 0,返回 null
if (inorder.Length == 0 || preorder.Length == 0)
return null;
// 从前序遍历序列中找到根节点的值
int rootVal = preorder[0];
// 在中序遍历序列中找到根节点对应的下标,以便分出左右部分
int rootIndex = Array.IndexOf(inorder, rootVal); // 提前为前序遍历的两部分分配内存空间
int[] preorderLeft = new int[rootIndex - 0];
int[] preorderRight = new int[inorder.Length - (rootIndex + 1)]; // 建立根节点
TreeNode root = new TreeNode(rootVal); // 拆分中序遍历序列
SplitArray(inorder, rootIndex, out int[] inorderLeft, out int[] inorderRight); // 拆分前序遍历序列
SplitArray(preorder, preorderLeft, preorderRight); // 递归地调用该方法以构建左右子树
root.left = BuildTree(preorderLeft, inorderLeft);
root.right = BuildTree(preorderRight, inorderRight); return root;
}
【题解二连发】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 traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...
- leetcode-1006 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 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [LeetCode-21]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 t ...
- 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 ...
- [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】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 ...
随机推荐
- Windows2008 R2 X64 PHP环境搭建步骤
Windows2008 R2 X64 PHP环境搭建步骤: 下载:Mysql5.7.23.PHP5.6.Zend.XCahe 一.安装MYSQL.导入数据: 解压MYsql压缩包,并新建Data目录, ...
- Oracle 外键级联更新
Oracle数据库中,外键约束只允许级联删除,不允许级联更新,因此,如果想要实现主表数据更新后,子表外键自动更新,只能取消外键关系,通过前端程序来维护实现完整引用,一个代替的解决方案是使用延迟约束和触 ...
- Azkaban 使用问题及解决(一)
什么是Azkaban Azkaban是一款基于Java编写的任务调度系统 任务调度:有四个任务脚A.B.C.D,其中任务A与任务B可以并行运行,然后任务C依赖任务A和任务B的运行结果,任务D依赖任务C ...
- App_Code目录类文件无法被调用的解决方法
1.选中类文件,在属性中的“生成操作”默认的“内容”改为“编译”就可以了. 2.重新生成解决方案
- ssl证书过期问题
问题:linux服务器ssl证书过期,申请新证书后,也更换了服务器的证书,但是网页一直提示证书过期 解决:经分析后,发现服务器架构为waf->slb->esc,域名并未直接解析到slb,解 ...
- JAVA的DES加密解密在windows上测试一切正常,在linux上异常
windows上加解密正常,linux上加密正常,解密时发生 如下异常,异常信息如下: [ERROR] 2018-10-15 09:30:35,998 method:com.iscas.ippc.co ...
- 后台文本编辑器KindEditor介绍
后台文本编辑器KindEditor介绍 我们在自己的个人主页添加文章内容的时候,需要对文章内容进行修饰,此时就需要文本编辑器助阵了! 功能预览 KindEditor文本编辑器 KindEditor文本 ...
- Docker使用Link与newwork在容器之间建立连接
一,使用 --link容器互联 docker 默认使允许container 互通的(通过-icc=false 关闭互通)同一个宿主机上的多个docker容器之间如果想进行通信,可以通过使用容器的ip地 ...
- 记录1-更换mac pro内存,硬盘及恢复系统
我的mac pro是2012年初买的,4G/500G HDD在服役了六年多后速度堪比老牛,以前装的虚拟机压根不敢打开.这几天把内存更换为8G,硬盘升级为samsung的1T SSD,感觉像起死回生一样 ...
- 如何让SQLServer的 itemNum 字段 按照数字大小顺序排序
我的 itemNum 从1到20,可是超过了SQLServer的默认排序这样的1101112...19234567如何才能让排序成为这样1234567891011.. . 解决办法:因为 itemNu ...