LeetCode Find Mode in Binary Search Tree
原题链接在这里:https://leetcode.com/problems/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.
For 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).
题解:
两遍Binary Tree Inorder Traversal.
第一遍找出有几个mode. 建立res array, 并保留了mode duplicate次数是多少. 第二遍当duplicate次数是mode 的duplicate次数时就加入res中.
Time Complexity: O(n).
Space: O(n), 每个节点都不同, res的size就是O(n). stack space O(logn). 如果利用Binary Tree Inorder Traversal中的Morris Traversal方法可以不用stack space.
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
int curVal;
int curCount;
int maxCount;
int modeNumber;
int [] res; public int[] findMode(TreeNode root) {
inorderTraversal(root);
res = new int[modeNumber];
curCount = 0;
modeNumber = 0;
inorderTraversal(root);
return res;
} private void inorderTraversal(TreeNode root){
if(root == null){
return;
} inorderTraversal(root.left);
handleCurrentNodeValue(root.val);
inorderTraversal(root.right);
} private void handleCurrentNodeValue(int val){
if(val != curVal){
curVal = val;
curCount = 0;
}
curCount++; if(curCount > maxCount){
maxCount = curCount;
modeNumber = 1;
}else if(curCount == maxCount){
if(res != null){
res[modeNumber] = curVal;
}
modeNumber++;
}
}
}
LeetCode Find Mode in Binary Search Tree的更多相关文章
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- [LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- 【一天一道LeetCode】#99. Recover Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Two ele ...
- 【一天一道LeetCode】#98. Validate Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- LeetCode OJ:Validate Binary Search Tree(合法的二叉搜索树)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
随机推荐
- Python学习进程(11)日期和时间
本节介绍Python应用程序处理时间和日期的方式.其中转换日期格式是最常用的功能. (1)获取时间戳: Python 提供了一个 time 和 calendar 模块可以用于格式化日期 ...
- MongoDB 使用Limit和Skip完成分页 和游标(二)
//$slice操作符返回文档中指定数组的内部值 //查询出Jim书架中第2~4本书 db.persons.find({name:"jim"},{books:{"$sli ...
- 在shell,R,python中用变量和常量创建文件名
很多时候我们希望文件名的格式是:变量+常量的. 1.shell:变量"常量" [wangjq@mgmt multi_pcr]$ a="var" [wangjq@ ...
- PHP实现链式操作
什么是链式操作 我们经常会在一些应用框架中看到如下代码: $db = new Database; $db->where('cid = 9')->order('aid desc')-> ...
- numpy模块之创建矩阵、矩阵运算
本文参考给妹子讲python https://zhuanlan.zhihu.com/p/34673397 NumPy是Numerical Python的简写,是高性能科学计算和数据分析的基础包,他是 ...
- 获取CPU利用率
#define MB (1024 * 1024) MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex); GlobalMemoryStatu ...
- Bootstrap3全局CSS样式
目录 1. 概览 2. 栅栏系统 3. 文本 4. 列表 5. 代码 6. 表格 7. 表单 7.1 基本实例 7.2 内联表单 7.3 水平排列的表单 8. 按钮 9. 图片 10. 辅助类 10. ...
- java格式化输出 printf 例子
import java.util.Date; public class Printf { public static void main(String[] args) { // %s表示输出字符串,也 ...
- Spring的AOP面向切面编程
什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...
- R语言笔记001——读取csv格式数据
读取csv格式数据 数据来源是西南财经大学 司亚卿 老师的课程作业 方法一:read.csv()函数 file.choose() read.csv("C:\\Users\\Administr ...