501. Find Mode in Binary Search Tree Easy
1/**
2 * [501] Find Mode in Binary Search Tree
3 *
4 * Given the root of a binary search tree (BST) with duplicates, return all the <a href="https://en.wikipedia.org/wiki/Mode_(statistics)" target="_blank">mode(s)</a> (i.e., the most frequently occurred element) in it.
5 * If the tree has more than one mode, return them in any order.
6 * Assume a BST is defined as follows:
7 *
8 * The left subtree of a node contains only nodes with keys less than or equal to the node's key.
9 * The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
10 * Both the left and right subtrees must also be binary search trees.
11 *
12 *
13 * Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/11/mode-tree.jpg" style="width: 142px; height: 222px;" />
15 * Input: root = [1,null,2,2]
16 * Output: [2]
17 *
18 * Example 2:
19 *
20 * Input: root = [0]
21 * Output: [0]
22 *
23 *
24 * Constraints:
25 *
26 * The number of nodes in the tree is in the range [1, 10^4].
27 * -10^5 <= Node.val <= 10^5
28 *
29 *
30 * Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
31 */
32pub struct Solution {}
33use crate::util::tree::{TreeNode, to_tree};
34
35// problem: https://leetcode.com/problems/find-mode-in-binary-search-tree/
36// discuss: https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40// Definition for a binary tree node.
41// #[derive(Debug, PartialEq, Eq)]
42// pub struct TreeNode {
43// pub val: i32,
44// pub left: Option<Rc<RefCell<TreeNode>>>,
45// pub right: Option<Rc<RefCell<TreeNode>>>,
46// }
47//
48// impl TreeNode {
49// #[inline]
50// pub fn new(val: i32) -> Self {
51// TreeNode {
52// val,
53// left: None,
54// right: None
55// }
56// }
57// }
58use std::rc::Rc;
59use std::cell::RefCell;
60impl Solution {
61 pub fn find_mode(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
62 vec![]
63 }
64}
65
66// submission codes end
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_501() {
74 }
75}
76
Back
© 2025 bowen.ge All Rights Reserved.