Sword refers to offer (2) Rebuild the binary tree + use two stacks to implement the queue + the smallest number of the rotating array

Record and review some question types of Jianzhi offer~


Rebuild the binary tree

Title description
Enter the results of pre-order traversal and middle-order traversal of a binary tree, and please rebuild the binary tree. Assume that the input result of pre-order traversal and middle-order traversal does not contain duplicate numbers. For example, input the pre-order traversal sequence {1,2,4,7,3,5,6,8} and the middle-order traversal sequence {4,7,2,1,5,3,8,6}, then rebuild the binary tree and return.


The first condition given to us is a pre-order traversal array and an in-order traversal array. It should be noted that for the array traversed in the middle order, the first value of the pre-order traversal is the middle value of the middle order traversal . So we can divide the middle-order traversal into small ranges by the order of the pre-order traversal, and then search through recursion.

Take a chestnut : Preorder traversal: {1,2,4,7,3,5,6,8}, the first value in 1 , traverse in middle order (4,7,2,1,5,3 ,8,6) is the exact middle value, so the in-order traversal can be divided into two arrays {4,7,2} and {5,3,8,6}. {4,7,2} array is the left side of 1 node, {5,3,8,6} array is the right side of 1 node, then only need to traverse the pre-order traversal of the array, will traverse to the first value Put it in the range of middle-order traversal. This is not to say that you really want to cut the array, just record the subscript of the cut array.
Of course, there are many better ways to be faster...Look at the code for details~

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
    
    
        //int [] pre 前序数组
        //int [] in  中序数组
        //获得前序比那里第一个元素
        TreeNode treeNode = new TreeNode(pre[0]);
        //设置下标为 0
        int index = 0;
        //遍历中序数组
        for (int i = 0; i < in.length; i++) {
    
    
            //找到对应的数字后退出 index记录下标
            if (pre[0] == in[i]){
    
    
                index = i;
                break;
            }
        }
        //调用方法,表示第一个元素的左子树在这个方法中返回 
        treeNode.left = ConstructProcess(pre, in, 0, index);
        treeNode.right = ConstructProcess(pre, in, index + 1, pre.length);
        //返回头结点
        return treeNode;
    }
        public static TreeNode ConstructProcess(int[] pre, int[]in, int begin, int end){
    
    
        //当输入的开始下标小于结束下标时继续
        if (begin < end){
    
    
            int index = 0;
            //设置外围循环的名字为outer
            outer:
            //遍历前序遍历数组
            for(int i:pre){
    
    
                //设置一个j下标作为是否在限定范围里的判断
                for (int j = 0; j < pre.length; j++) {
    
    
                    //如果j在范围里并且i的值正好能在中序数组中找到
                    if (j>= begin && j <end && i == in[j]){
    
    //设置下标为j
                        index = j;
                        //退出外循环
                        break outer;
                    }
                }
            }
            //设置当前节点为中序遍历找到的节点
            TreeNode treeNode= new TreeNode(in[index]);
            //递归获取左子树和右子树
            treeNode.left = ConstructProcess(pre, in, begin, index);
            treeNode.right = ConstructProcess(pre, in, index + 1, end);
            //返回当前节点
            return treeNode;
        }
        //不然返回null
        return null;
    }
}

Implement the queue with two stacks

Title description
Use two stacks to implement a queue to complete the Push and Pop operations of the queue. The elements in the queue are of type int.


Two stacks are used to implement a queue's addition and pop operations. Then we can create two stacks, stack 1 as the stack of push elements, stack 2 as the stack of pop elements, and when stack 2 is empty, pop the elements in stack 1 to stack 2.

Knowledge supplement : how to implement a queue with two stacks? Just add the elements that need to be passed in to the first stack, then pop from the first stack, and then add to the second stack, so that the elements popped out of the second stack are in the form of a queue. Do this question on this basis.

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.add(node);
    }
    
    public int pop() {
    
    
       //当第二个栈为空时
       if (stack2.size() == 0){
    
    
           while (stack1.size() != 0){
    
    
               //只要第一个栈不为空,就将剩余元素都放到二号栈中
               stack2.push(stack1.pop());
           }
       }
        //进行二号栈的pop()
        return stack2.pop();
    }
}

Rotate the smallest number of the array

Title description
Moving the first elements of an array to the end of the array is called the rotation of the array.
Input a rotation of a non-decreasing sorted array, and output the smallest element of the rotated array.
For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1.
NOTE: All elements given are greater than 0. If the array size is 0, please return 0.


…The topic seems very complicated, and I thought about it for a while while doing it. The original intention should be to give an ordered array, then replace the next n numbers to the front, and then output the minimum value. After thinking about recording the first value max, then traversing to find the first value smaller than max is the answer.

import java.util.ArrayList;
public class Solution {
    
    
    public int minNumberInRotateArray(int [] array) {
    
    
        //当数组为0,则返回
        if(array.length == 0) return 0;
        //设置第一个值为max
        int max = array[0];
        //从第二个值开始遍历
        for(int i=1;i<array.length;i++){
    
    
            //当max大于这个值时,说明这个值就是最小值,因为这是有序数组
            if(max > array[i]){
    
    
                return array[i];
            }
        }
        return max;
    }
}

Guess you like

Origin blog.csdn.net/qq_41762594/article/details/106021667