- 确定根节点的值
- 递归构造左右子树
654. 最大二叉树
给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:
二叉树的根是数组中的最大元素。
左子树是通过数组中最大值左边部分构造出的最大二叉树。
右子树是通过数组中最大值右边部分构造出的最大二叉树。
通过给定的数组构建最大二叉树,并且输出这个树的根节点。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
//根据题意,根节点是最大值,要遍历找最大元素naxVal,这里可以对maxVal左右递归
if(nums.length == 0) return null;
return build(nums, 0, nums.length - 1);
}
private TreeNode build(int[] nums, int lo, int hi){
if(lo > hi) return null;
//1.找最大值和其对应索引
int index = -1, maxValue = Integer.MIN_VALUE;
for(int i = lo; i <= hi; i++){
if(nums[i] > maxValue){
index = i;
maxValue = nums[i];
}
}
TreeNode root = new TreeNode(maxValue);
//2.递归构造左右子树
root.left = build(nums, lo, index - 1);
root.right = build(nums, index + 1, hi);
return root;
}
}
105. 从前序与中序遍历序列构造二叉树
根据一棵树的前序遍历与中序遍历构造二叉树。
参看:https://mp.weixin.qq.com/s/OlpaDhPDTJlQ5MJ8tsARlA

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
//确定根节点的值,递归左右子树
//前序遍历第一个节点是根节点
return build(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
}
//若前序遍历数组为 preorder[preStart..preEnd],
//后续遍历数组为 postorder[postStart..postEnd],
//构造二叉树,返回该二叉树的根节点
private TreeNode build(int[] preorder,int preStart, int preEnd,int[] inorder,int inStart, int inEnd){
if(preStart > preEnd) return null;
//preorder第一个元素是root节点
int rootVal = preorder[preStart];
//在inorder中找到rootVal索引
int index = 0;
for(int i = 0; i <= inEnd; i++){
if(rootVal == inorder[i]){
index = i;
break;
}
}
int leftSize = index - inStart;
TreeNode root = new TreeNode(rootVal);
//构造左右子树
root.left = build(preorder, preStart+1, preStart+leftSize, inorder, inStart, index-1);
root.right = build(preorder, preStart+leftSize+1, preEnd, inorder, index+1, inEnd);
return root;
}
}
106. 从中序与后序遍历序列构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树。
参看:https://mp.weixin.qq.com/s/OlpaDhPDTJlQ5MJ8tsARlA

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return build(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1);
}
private TreeNode build(int[] inorder, int inStart, int inEnd, int[] postorder, int postStart, int postEnd){
if(inStart > inEnd) return null;
//根节点的值是后序遍历的最后一个
int rootVal = postorder[postEnd];
//找到根节点的下标
int index = 0;
for(int i = 0; i <= inEnd; i++){
if(inorder[i] == rootVal){
index = i;
break;
}
}
int leftSize = index - inStart;
//构造根节点
TreeNode root = new TreeNode(rootVal);
//构造左右子树
root.left = build(inorder, inStart, index-1, postorder, postStart, postStart+leftSize-1);
root.right = build(inorder, index+1, inEnd, postorder, postStart+leftSize, postEnd-1);
return root;
}
}
注:105,106都是找中序遍历中的索引,之后画图找递归索引。
本文介绍了如何根据数组构建最大二叉树,以及如何从给定的前序、中序、后序遍历序列构造二叉树。详细解析了654. 最大二叉树问题,以及105. 和106. 两种不同遍历序列的构造方法,提供了相关链接进行深入学习。
472

被折叠的 条评论
为什么被折叠?



