Likou 501 Mode in Binary Search Tree

Original title link: https://leetcode-cn.com/problems/find-mode-in-binary-search-tree

The content of the topic is as follows:

Given a binary search tree (BST) with the same value, find all modes (the most frequent 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
  • 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
  • Both left subtree and right subtree are binary search trees

For example:
given BST [1,null,2,2],

   1
    \
     2
    /
   2
return to [2].

Tip: If the mode exceeds 1, no need to consider the output order

Advanced: Can you not use extra space? (Assuming that the overhead of the implicit call stack generated by recursion is not included in the calculation)

 

        Binary sorting tree, also known as binary search tree, binary sorting tree, from the data structure and title we can know that all nodes of the left subtree of any node are less than or equal to its value, and all nodes of the right subtree are greater than or equal to its value. Value, of course, the most critical property is that the results of the middle-order traversal of the binary sort tree are ordered, the results of the middle-order traversal of the binary sort tree are ordered, and the results of the middle-order traversal of the binary sort tree are ordered . Therefore, for this problem, after applying this property, it becomes easy to solve: to find the mode of the binary sort tree, it can be simplified to traverse the result of its order, such as [1,2,3,3,3,4,4 ,7], that is, the position where each number appears is continuous, and the number with the most consecutive occurrences is placed in the final result. Obviously, if the extra space is not considered, then the recursive or non-recursive method of in-order traversal can be used to save the result (root node value) of each output in a data structure such as an array or queue, and then traverse sequentially to obtain .

        But the advanced requirement is that no extra space is used, but recursion can still be used, so it can be judged dynamically instead of keeping all the output results:

        Set the variables Res, MaxNum, CurNum, CurVal used to save temporary results, meaning as follows. (The idea of ​​the title does not clearly indicate the size of the node value, the default is int)

variable name Types of Paraphrase
Res vector<int> Save all current modes
maxNum int The number of occurrences of the mode corresponding to the current Res
curne int The number of occurrences of the previous node value
CurVal int Previous node value

        Each time the value of the current node is obtained recursively, the judgment is made in order:

        1. Whether it is consistent with the value of the previous node (parent node), if it is the same, then the number of occurrences of the previous node value CurNum increases by 1; otherwise, update CurVal to the value of the current node, and set CurNum to 1.

        2. Whether the updated CurNum is greater than MaxNum, if it is greater, Res is cleared (there is already a number with more occurrences than the current "temporary mode" with the most occurrences, then the new number is the "temporary mode"), and Put the current CurVal into Res, and set MaxNum to CurNum; if it is equal, it means that the node value is also the "temporary mode", and put the current CurVal into Res.

        So until the end of the recursion, the value contained in Res is the final result.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> Res;//储存众数
    int MaxNum=0;//Res中储存的最大次数
    int CurNum=0;//上次遍历数字出现次数
    int CurVal=-1;//上次遍历数字
    void LDR(TreeNode* root){
        if(root==NULL){
            return ;
        }
        else{
            LDR(root->left);
            int IterVal=root->val;//本次得到的值
            if (IterVal!=CurVal){//如果本次的值与上一次的值不等
                CurVal=IterVal;//更新
                CurNum=1;

            }
            else{//相等则加一
                CurNum+=1;
                }
            if (CurNum==MaxNum){//次数与最多次数相等,则放入
                Res.push_back(CurVal);
            }
            else if (CurNum>MaxNum){//大于则重置
                Res.clear();
                MaxNum=CurNum;
                Res.push_back(CurVal);
            }

            LDR(root->right);
        }
    }
    vector<int> findMode(TreeNode* root) {
        LDR(root);
        return Res;
    }
};

 

 

Guess you like

Origin blog.csdn.net/qq_36614557/article/details/108773103