[leetcode]: 501. Find Mode in Binary Search Tree
来源:互联网 发布:qq刷会员软件 编辑:程序博客网 时间:2024/06/07 01:00
1.题目
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].
给一棵二叉搜索树,有重复元素。找出其中重复次数最多的元素。如果有多个重复次数一样的元素,则放在一个列表中作为返回结果。
2.分析
这题考察的是BST的中序遍历,中序遍历的元素是有序的,在遍历的过程中统计元素出现的次数即可。
3.代码
中序遍历–迭代
/** * 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> findMode(TreeNode* root) { vector<int> mode; if (root == NULL) return mode; int max_c = -1;//记录历史最多出现次数 int last_v = INT_MIN; int count = 1;//当前值出现的次数 stack<TreeNode*> nodes; while (!nodes.empty() || root) { if (root) { nodes.push(root); root = root->left;//先遍历到左子树最下边 } else { root = nodes.top(); nodes.pop(); if (last_v != INT_MIN) { if (root->val == last_v) ++count; else count = 1; } if (count > max_c) { max_c = count; mode.erase(mode.begin(), mode.end()); mode.push_back(root->val); } else if (count == max_c) mode.push_back(root->val); last_v = root->val; root = root->right;//转到右子树 } } return mode; }};
中序遍历–递归
class Solution {public: void inOrder(TreeNode* root, vector<int>&mode,int& val,int& count,int& maxc) { if (root == NULL) return; inOrder(root->left,mode,val,count,maxc); if (val != INT_MIN) { if (val == root->val) ++count; else count = 1; } if (count > maxc) { maxc = count; mode.erase(mode.begin(), mode.end());//出现次数更多的元素,清空容器 mode.push_back(root->val); } else if (count == maxc) mode.push_back(root->val); val = root->val; inOrder(root->right, mode, val, count, maxc); } vector<int> findMode(TreeNode* root) { vector<int> mode; if (root == NULL) return mode; int maxc = -1; int count = 1; int val = INT_MIN; inOrder(root, mode, val, count, maxc); return mode; }};
阅读全文
0 0
- [Leetcode] Binary tree -- 501. Find Mode in Binary Search Tree
- LeetCode 501. Find Mode in Binary Search Tree
- leetcode 501. Find Mode in Binary Search Tree
- LeetCode-501. Find Mode in Binary Search Tree
- Leetcode——501. Find Mode in Binary Search Tree
- LeetCode 501.Find Mode in Binary Search Tree
- LeetCode 501. Find Mode in Binary Search Tree
- [LeetCode] 501. Find Mode in Binary Search Tree 解题报告
- [LeetCode]501. Find Mode in Binary Search Tree
- [leetcode: Python]501. Find Mode in Binary Search Tree
- [leetcode]: 501. Find Mode in Binary Search Tree
- leetcode 501. Find Mode in Binary Search Tree
- [LeetCode] 501. Find Mode in Binary Search Tree
- [leetcode]501. Find Mode in Binary Search Tree
- LeetCode-501. Find Mode in Binary Search Tree(Java)
- leetcode 501. Find Mode in Binary Search Tree
- leetcode 501. Find Mode in Binary Search Tree
- [Leetcode] 501. Find Mode in Binary Search Tree 解题报告
- 工作流引擎Activiti学习---使用流程变量进行个人任务分配
- Android 广播(Receiver)的动态注册
- Go web编程学习(一)
- 背包问题
- Android Priority Job Queue (Job Manager):后台线程任务结果传回前台(三)
- [leetcode]: 501. Find Mode in Binary Search Tree
- (数据结构课程设计)稀疏矩阵运算器
- ListView
- 快速看懂ply格式
- 在C#中如何获取字符串的真实的长度
- AlphaGo已经拿下围棋,创投界的“Master”何时出现?
- javaMail 的使用,java代码发送一份邮件
- Kotlin入门____基础语法(一)
- Android 广播 动、静态注册广播(作为内部类使用)