LeetCode-501. Find Mode in Binary Search Tree

Description

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.

Assume a BST is defined as follows:

 - The left subtree of a node contains only nodes with keys less than or equal to the node's key.
 - The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
 - Both the left and right subtrees must also be binary search trees.

Example

Given BST [1,null,2,2],
   1
    \
     2
    /
   2
return [2].

Note

If a tree has more than one mode, you can return them in any order.

Follow Up

Could you do that without using any extra space? (Assume that the implicit stack space incurred due 
to recursion does not count).

Solution 1(C++)

static int x=[](){std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }();
class Solution {
public:
    void travel(TreeNode* node){
        if(!node) return;
        mode[node->val]++;
        travel(node->right);
        travel(node->left);
    }
    vector<int> findMode(TreeNode* root) {
        travel(root);
        vector<int> res;
        for(auto iter=mode.begin(); iter!=mode.end(); iter++){
            if(!res.size()) res.push_back(iter->first);
            else{
                if(iter->second > mode[res[0]]){
                    res.clear(); res.push_back(iter->first);
                }
                else if(iter->second == mode[res[0]]) res.push_back(iter->first);
                else continue;
            }
        }
        return res;
    }
private:
    map<int, int> mode;
};

Solution 2(C++)

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        vector<int>res;
        TreeNode* pre= NULL;
        int gmax = INT_MIN;
        int cnt=0;
        helper(root,res,pre,cnt, gmax);
        return res;
    }

    void helper(TreeNode* root, vector<int>& res, TreeNode* &pre, int &cnt, int &gmax){
        if(!root) return;

        helper(root->left,res,pre,cnt,gmax);

        if(pre!= NULL){
            cnt = pre->val == root->val ? cnt+1: 1;
        }
        else
            cnt++;

        if(cnt >= gmax ){
            if(cnt > gmax) res.clear();
            res.push_back(root->val);
            gmax = cnt;
        }
        pre = root;

        helper(root->right,res,pre,cnt,gmax);
    }
};

算法分析

解法一,暴力解法。解法二,比较符合题目的要求。但目前还没有看太懂。

现在明白了,这就是利用二叉树的中序遍历的方法,将BST转换成一个排序好的数组。可参考:二叉树前序、中序、后序遍历的迭代实现

二叉树的三种遍历方法真的非常经典,我准备单独用一篇博客来学习一下。到时候也会在这篇博客中进行更新。

程序分析

略。

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值