leetcode_树_501. Mode in binary search tree(#)

Hello, everyone, I am a pig who is dominated by cabbage.

A person who loves to study, sleepless and forgets to eat, is obsessed with the girl's chic, calm and indifferent coding handsome boy.

1. Subject content

Given a binary search tree (BST) with the same value, find all modes (the most frequently occurring element) in the BST.
Assume that BST has the following definition:
the value of the node contained in the left subtree of the node is less than or equal to the value of the current node, and the value of the
node contained in the right subtree of the node is greater than or equal to the value of the current node.
Left subtree and right subtree trees are binary search tree
example:
given BST [1, null, 2, 2],
1
\
2
/
2
returns [2].
Tip: If the number of the congregation more than one, without regard to the output sequence
Advanced: you Can I not use the extra space? (Assuming that the overhead of the implicit call stack generated by recursion is not included in the calculation)

Two, problem-solving ideas

After reading this question, there are two ways to solve the problem:
Idea 1:
Traverse, use HashMap to record the number of occurrences of each value, key is the val of the node, value is count, and finally traverse the hashMap to find the maximum number of occurrences, and then Traverse again, find val==max and add it to the array.
Idea 2:
Using the characteristics of the binary search tree, the middle order traversal of the binary search tree is arranged in ascending order, so the value of the previous node is recorded with a value, and then compared with the current node value, if the same, count++, and then compared with max , The initial value of max is 0. If count is greater than max, it is added to the list. Before adding, the list must be cleared, and then max=count. There is a situation here, if there are the same number, it does not need to be cleared.

Three, code implementation

class Solution {
    
    
    private List<Integer> list = new ArrayList<>();
    private int max=0;//记录上一次最大的值
    private int count = 1;
    private int last = -1;


    public int[] findMode(TreeNode root) {
    
    
        inOrder(root);
        int[] res = new int[list.size()];
        for(int i=0;i<list.size();i++){
    
    
            res[i] = list.get(i);
        }
        return res;
    }

    public  void inOrder(TreeNode root) {
    
    
        if(root == null) return;
        //二叉搜索树中序遍历是升序排列的
        inOrder(root.left);
        //在这处理数据
        //先获得当前值,然后与上一节点的值进行比较,加入相同则count+1,不同则说明是新的值
        int cur = root.val;
        if(cur == last) {
    
    
            count++;
        }else{
    
    
            count = 1;
        }

        if(count > max) {
    
    
            list.clear();
            list.add(cur);
            max = count;
        }
        if(count == max && list.get(list.size() - 1) != cur) list.add(cur); //以为上面max=count,所以这里要在加一个条件限制
        last = cur;
        inOrder(root.right);

    }
}

The middle section

if(count == max && list.get(list.size() - 1) != cur) list.add(cur);

If you remove it and put it in front of the judgment, you will find that the efficiency is no longer 100%. Here you need to think about why.

Guess you like

Origin blog.csdn.net/weixin_44226263/article/details/109366487