04 重建二叉树
🍒题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},
则重建二叉树并返回。
思路:递归
🍒前序遍历的性质:根据前序序列第一个元素是根节点root
🍒中序遍历的性质:根据根节点在中序序列中位置r分割出左右两个子序列对左子树和右子树分别递归使用同样的方法继续分解。
例如:
前序序列{1,2,4,7,3,5,6,8} = pre
中序序列{4,7,2,1,5,3,8,6} = in
根据当前前序序列的第一个结点确定根结点为 1,找到 1 在中序遍历序列中的位置为 in[3]。
切割左右子树,则 in[3] 前面的为左子树, in[3] 后面的为右子树,所以:
切割后的左子树前序序列为:{2,4,7},
切割后的左子树中序序列为:{4,7,2};
切割后的右子树前序序列为:{3,5,6,8},
切割后的右子树中序序列为:{5,3,8,6};
对子树分别继续使用同样的方法分解。
题解1 变量多,易理解
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.Arrays;//⚠️注意
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if(pre.length == 0){
return null;
}
int rootVal = pre[0];//据前序遍历的性质第一个元素是root元素
if(pre.length == 1){
return new TreeNode(rootVal);
}
TreeNode root = new TreeNode(rootVal);
int rootIndex = 0;
for(int i = 0;i < in.length;i++){//检查root元素在中序遍历
if(rootVal == in[i]){
rootIndex = i;
break;
}
}
//递归
root.left = reConstructBinaryTree(Arrays.copyOfRange(pre,1,rootIndex + 1),Arrays.copyOfRange(in,0,rootIndex));//注意 copyOfRange 函数,左闭右开
root.right = reConstructBinaryTree(Arrays.copyOfRange(pre,rootIndex + 1,pre.length),Arrays.copyOfRange(in,rootIndex + 1,in.length));
return root;
}
}
题解2 变量少,整合方法
(其实至少了一个变量haha)
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.Arrays;
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if(pre.length == 0){
return null;
}
int rootVal = pre[0];//据前序遍历的性质第一个元素是root元素
if(pre.length == 1){
return new TreeNode(rootVal);
}
TreeNode root = new TreeNode(rootVal);
// int rootIndex = 0;
for(int i = 0;i < in.length;i++){//检查root元素在中序遍历
if(rootVal == in[i]){
//rootIndex = i;
root.left = reConstructBinaryTree(Arrays.copyOfRange(pre,1,i + 1),Arrays.copyOfRange(in,0,i));
root.right = reConstructBinaryTree(Arrays.copyOfRange(pre,i + 1,pre.length),Arrays.copyOfRange(in,i + 1,in.length));
break;
}
}
return root;
}
}
05 两个栈来实现一个队列
🍒题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
思路1:
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if(stack2.size() <= 0){
while(stack1.size() != 0){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
思路2:左程云的《程序员代码面试指南》的答案
A与B为空时,此时队列为空,出队操作不能执行; A为空但是B不为空,此时入队操作,应该把栈B中的元素移回A,再执行A入栈
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if(stack1.empty()&&stack2.empty()){
throw new RuntimeException("Queue is empty!");
}
if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
06 旋转数组的最小数字
🍒题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
非递减:递增数组
### 思路1:直接查找(时间复杂度O(n))
根据题意:旋转前为递增数组,旋转后数组可以划分为两个有序的子数组,前面子数组的大小都大于后面子数组中的元素,最小的元素就是两个子数组的分界线。
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
int n = array.length;
if (n == 0){
return 0;
}
for(int i = 0;i < n - 1;i++)
{
if(array[i] > array[i+1])
{
return array[i+1];//找到分界处,即为min
}
}
return array[0];//纯递增,首元素为min
}
}
### 思路2:二分查找法(😕)
旋转之后的数组实际上可以划分成两个有序的子数组:前面子数组的大小都大于后面子数组中的元素,注意到实际上最小的元素就是两个子数组的分界线。
中间元素大于第一个元素,则中间元素位于前面的递增子数组,此时最小元素位于中间元素的后面。我们可以让第一个指针left指向中间元素。移动之后,第一个指针仍然位于前面的递增数组中。
中间元素小于第一个元素,则中间元素位于后面的递增子数组,此时最小元素位于中间元素的前面。我们可以让第二个指针right指向中间元素。移动之后,第二个指针仍然位于后面的递增数组中。
import java.util.*;
public class Solution {
public int minNumberInRotateArray(int [] array) {
int i = 0,j = array.length - 1;
while (i < j)
{
if(array[i] < array[j])
{
return array[i];
}
int mid = (i +j) >> 1;
if(array[mid] > array[i]){
i = mid + 1;
}
else if(array[mid] < array[j])
{
j = mid;//如果是mid-1,则可能会错过最小值,因为找的就是最小值
} //巧妙避免了offer书上说的坑点(1 0 1 1 1)
else
i++;
}
return array[i];
}
}
### 思路3:Arrays.sort(array);或者 优先队列
PriorityQueue<Integer> queue = new PriorityQueue<>();
for(int i = 0;i<n;i++)
{
queue.add(array[i]);
}
return queue.poll();
借鉴总结自牛客网