力扣labuladong一刷day30天二叉树
文章目录
-
-
- 力扣labuladong一刷day30天二叉树
- [一、654. 最大二叉树](#一、654. 最大二叉树)
- [二、105. 从前序与中序遍历序列构造二叉树](#二、105. 从前序与中序遍历序列构造二叉树)
- [三、106. 从中序与后序遍历序列构造二叉树](#三、106. 从中序与后序遍历序列构造二叉树)
- [四、889. 根据前序和后序遍历构造二叉树](#四、889. 根据前序和后序遍历构造二叉树)
-
一、654. 最大二叉树
题目链接:https://leetcode.cn/problems/maximum-binary-tree/
思路:采用分解问题的方法,先构造父节点再构造左右子节点,每次都在指定区间内搜索,找到最大值后构造父节点,再划分左右区间递归。
java
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return traverse(nums, 0, nums.length - 1);
}
TreeNode traverse(int[] nums, int left ,int right) {
if (left > right) return null;
int max = -1, index = -1;
for (int i = left; i <= right; i++) {
if (nums[i] > max) {
max = nums[i];
index = i;
}
}
TreeNode root = new TreeNode(max);
root.left = traverse(nums, left, index-1);
root.right = traverse(nums, index+1, right);
return root;
}
}
二、105. 从前序与中序遍历序列构造二叉树
题目链接:https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
思路:通过前序和中序构造二叉树,要维护好前序的区间以及中序的区间,先构造父节点再构造左右子节点,父节点是前序区间的left,左右子节点由递归返回。 此外要注意速度,想节省遍历中序数组寻找父节点索引的时间的话,可以使用map预先存储下来。
java
class Solution {
Map<Integer, Integer> map = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}
return create(preorder, inorder, 0, preorder.length-1,0, inorder.length-1);
}
TreeNode create(int[] preorder, int[] inorder, int left1, int right1, int left2, int right2) {
if (left1 > right1 || left2 > right2) return null;
int midV = preorder[left1];
TreeNode node = new TreeNode(midV);
int index = map.get(midV);
node.left = create(preorder, inorder, left1+1, left1+index-left2, left2, index-1);
node.right = create(preorder, inorder, left1+index-left2+1, right1, index+1, right2);
return node;
}
}
三、106. 从中序与后序遍历序列构造二叉树
题目链接:https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
思路:本题和上题类似,也是先构造父节点,然后再构造左右子节点,父节点由后续right构造,然后划分中序和后序的区间,递归构造左右子树。
java
class Solution {
Map<Integer, Integer> map = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}
return create(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1);
}
TreeNode create(int[] inorder, int[] postorder, int left1, int right1, int left2, int right2) {
if (left1 > right1 || left2 > right2) return null;
int midV = postorder[right2];
int index = map.get(midV);
TreeNode node = new TreeNode(midV);
node.left = create(inorder, postorder, left1, index-1, left2, left2+index-left1-1);
node.right = create(inorder, postorder, index+1, right1, left2+index-left1, right2-1);
return node;
}
}
四、889. 根据前序和后序遍历构造二叉树
题目链接:https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-postorder-traversal/
思路:通过前序和后序去构造二叉树,构造的结果不唯一,但方法是一样的,每次使用前序的left做为父节点,left+1做为左孩子的值,然后使用这个去后序中去划分区间,然后在划分前序遍历的区间,之后递归构造即可。
java
class Solution {
Map<Integer, Integer> map = new HashMap<>();
public TreeNode constructFromPrePost(int[] preorder, int[] postorder) {
for (int i = 0; i < postorder.length; i++) {
map.put(postorder[i], i);
}
return create(preorder, postorder, 0, preorder.length-1, 0, postorder.length-1);
}
TreeNode create(int[] preorder, int[] postorder, int left1, int right1, int left2, int right2) {
if (left1>right1 || left2>right2) return null;
if (left1 == right1) return new TreeNode(preorder[left1]);
int mid = preorder[left1];
int index = map.get(preorder[left1+1]);
TreeNode node = new TreeNode(mid);
node.left = create(preorder, postorder, left1+1, left1 + index-left2+1, left2,index);
node.right = create(preorder, postorder, left1 + index-left2+2, right1,index+1, right2-1);
return node;
}
}