【Java Trainee】Learn to punch in the daily interview questions!

Check-in Day02-------------2022/6/25

Leetcode daily question:

Problem: Find the maximum value in each tree row

insert image description here

Idea: This question is actually a question of layer order traversal, but with a little modification. There are still two ways to solve problems: recursion and iteration.

code:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    
    //方法一:迭代方法
    public List<Integer> largestValues(TreeNode root) {
    
    
        List<Integer> res = new ArrayList<>();
        if(root==null) return res;
        Queue<TreeNode> path = new LinkedList<>();
        path.offer(root);
        while(!path.isEmpty()){
    
    
            int size = path.size();
            int max = Integer.MIN_VALUE;
            while(size>0){
    
    
                TreeNode node = path.poll();
                max=Math.max(node.val,max);
                if(node.left!=null) path.offer(node.left);
                if(node.right!=null) path.offer(node.right);
                size--;
            }
            res.add(max);
        }
        return res;

    }
	//方法二:递归方法
    public List<Integer> largestValues(TreeNode root) {
    
    
        if (root == null) {
    
    
            return new ArrayList<Integer>();
        }
        List<Integer> res = new ArrayList<Integer>();
        dfs(res, root, 0);
        return res;
    }

    public void dfs(List<Integer> res, TreeNode root, int curHeight) {
    
    
        if (curHeight == res.size()) {
    
    
            res.add(root.val);
        } else {
    
    
            res.set(curHeight, Math.max(res.get(curHeight), root.val));
        }
        if (root.left != null) {
    
    
            dfs(res, root.left, curHeight + 1);
        }
        if (root.right != null) {
    
    
            dfs(res, root.right, curHeight + 1);
        }
    }
}

Topic 1: Object-Oriented Features

answer:

1. Encapsulation: Abstract objective things into classes, and each class protects its own data and methods. A class can only let trusted classes or objects operate on its own data and methods, and hide information from untrusted ones.

2. Inheritance: Inheritance is a hierarchical model of linking classes, and allows and encourages the reuse of classes. It provides a clear way to express commonality. A new class of objects can be derived from an existing class, a process called class inheritance. The new class inherits the characteristics of the original class, the new class is called the derived class of the original class, and the original class is called the base class of the new class. A derived class can inherit methods and instance variables from its base class, and the derived class can modify or add new methods to make it more suitable for special needs.

3. Polymorphism: Polymorphism refers to allowing different types of objects to respond to the same message. Polymorphism includes parametric polymorphism and containment polymorphism. Polymorphic language has the advantages of flexibility, abstraction, behavior sharing, code sharing, etc., and solves the problem of the same name of application functions.

4. Abstraction: Abstraction is to ignore those aspects of a topic that are irrelevant to the current goal, so as to fully pay attention to the aspects related to the current goal. Abstraction does not intend to understand all problems, but to select some of them and leave some details for the time being. Abstraction includes two aspects: process abstraction and data abstraction.

Topic 2: The difference between String, Stringbuffer, and StringBuilder

answer:

String: This class is an immutable class and cannot be modified once created. String is a final class and cannot be inherited. String implements equals() and hashCode() methods

StringBuffer: Inherited from AbstractStringBuilder, it is a variable class. StringBuffer is thread safe. Data can be dynamically constructed through the append method.

StringBuilder: Inherited from AbstractStringBuilder, it is a variable class. StringBuilder is nonlinear safe. The execution efficiency is higher than StringBuffer.

Topic 3: The difference between overloading and rewriting

answer:

  1. Rewriting must be inherited, overloading is not.
  2. Overloading means that there can be multiple methods with the same name in the same class, but the parameter lists of these methods are different (that is, the number or types of parameters are different)
  3. Rewriting means that the method in the subclass has exactly the same name and parameters as a method in the parent class. When this method is called through the instance object of the subclass, the defined method in the subclass will be called, which is equivalent to calling the method in the parent class. The exact same method defined is covered, which is a manifestation of polymorphism in object-oriented programming.
  4. The overridden method modifier is greater than or equal to the method of the parent class, that is, the access permission can only be greater than that of the parent class, and cannot be smaller, and the overload has nothing to do with the modifier
  5. In the overridden method, only fewer exceptions can be thrown than the parent class, or the sub-exception of the exception thrown by the parent class can be thrown.

The above is all the content of today's check-in study. I hope everyone will work hard with me and get the offer softly.

Guess you like

Origin blog.csdn.net/qq_44085437/article/details/125453945