LeetCode题解之 Find Mode in Binary Search Tree
1、题目描述
2、问题分析
使用map记录元素出现的次数。
3、代码
vector<int> v;
map<int,int> m;
vector<int> findMode(TreeNode* root) {
if (root == NULL)
return v;
preOrder(root);
int n = ;
for(map<int,int>::iterator it = m.begin(); it != m.end(); it++) {
if (it->second >= n)
n = it->second;
} for(map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
if (it->second == n)
v.push_back(it->first);
} return v;
} void preOrder(TreeNode *root)
{
if (root != NULL)
m[root->val]++;
else
return;
preOrder(root->left);
preOrder(root->right);
}
LeetCode题解之 Find Mode in Binary Search Tree的更多相关文章
- LeetCode题解之Insert into a Binary Search Tree
1.题目描述 2.分析 插入算法. 3.代码 TreeNode* insertIntoBST(TreeNode* root, int val) { insert(root, val); return ...
- [LeetCode 题解]:Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告
https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...
- LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目 ...
- 【leetcode刷题笔记】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【leetcode刷题笔记】Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- 机器学习技法笔记:01 Linear Support Vector Machine
Roadmap Course Introduction Large-Margin Separating Hyperplane Standard Large-Margin Problem Support ...
- SeaweedFS上手使用指南
SeaweedFS是基于go语言开发的高可用文件存储系统,主要特性 1.成存储上亿的文件(最终受制于你的硬盘大小)2.速度快,内存占用小 上手使用比fastDFS要简单很多,自带Rest API. S ...
- [EXP]phpBB 3.2.3 - Remote Code Execution
// All greets goes to RIPS Tech // Run this JS on Attachment Settings ACP page var plupload_salt = ' ...
- Comparable接口和Comparator接口的不同用法
两者都可用来在定义比较方法,然后用在排序中. Comparable是类本身继承的接口 Comparator实在类外定义一个排序的类 比较而言,觉得Comparator更灵活一些,但是Comparabl ...
- Android 源码分析01_AsyncTask
[参考文献] http://blog.csdn.net/singwhatiwanna/article/details/17596225 /* * Copyright (C) 2008 The Andr ...
- jar包版本介绍(beta,alpha,release),软件的版本介绍
α(Alpha) 此版本表示该软件仅仅是一个初步完成品,通常只在软件开发者内部交流,也有很少一部分发布给专业测试人员.一般而言,该版本软件的bug(漏洞)较多,普通用户最好不要安装.主要是开发者自己对 ...
- 解决Windows英文版中文软件乱码的问题
由于工作的原因,我们的开发环境都是基于英文的,但是在打开一些基于中文编码的软件的时候经常会出现乱码的情况.其实只要打开控制面板按照下面的步骤稍微设置一下,就可以完美的在英文操作系统下支持中文软件了,因 ...
- docker学习系列(二):使用Dockerfile创建自己的镜像
dockerfile可以允许我们自己创建镜像,通过编写里面的下载软件命令,执行docker build 即可生成镜像文件. 初尝dockerfile 新建一个目录test,然后进入这个目录,创建一个名 ...
- centos7环境开启WIFI热点
1.环境介绍 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@localhost ~]# u ...
- 自动化发布-GitLab WEB Hooks 配置
钩子(hooks) hooks是在特定事件发生之前或之后执行特定脚本代码功能(从概念上类比,就与监听事件.触发器之类的东西类似). Git hooks就是那些在Git执行特定事件(如commit.pu ...