501. Find Mode in Binary Search Tree

Find Mode in Binary Search Tree (LeetCode 501) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Inorder Traversal subpattern.

How to solve Find Mode in Binary Search Tree

Find Mode in Binary Search Tree is a Inorder Traversal problem, part of Tree Traversal Patterns (DFS & BFS). Master inorder traversal for BST validation and kth element problems. Produces sorted order in BST.

Work towards the solution in these steps:

  1. Consider traversing the binary search tree to access all node values systematically.
  2. Since it's a BST, think about how an inorder traversal can help access values in a sorted order.
  3. While traversing, keep track of the frequency of the current value and compare it with the maximum frequency encountered so far.

The reference solution runs in O(n) time and O(h) space.

The Find Mode in Binary Search Tree solution and editorial walks through the full approach with reference code.

Problem statement

Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.

If the tree has more than one mode, return them in any order.

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 1:

Input: root = [1,null,2,2] Output: [2]

Example 2:

Input: root = [0] Output: [0]

Constraints:

- The number of nodes in the tree is in the range [1, 10^4].

- -10^5 <= Node.val <= 10^5

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

Topics and companies

Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree.

Reported in interviews at Google.

How to practise Find Mode in Binary Search Tree on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(h) space.

Open Find Mode in Binary Search Tree in the code editor, or read the Find Mode in Binary Search Tree editorial for a worked solution with its approach and complexity analysis.

Where Find Mode in Binary Search Tree sits in the DSA pattern sheet

Problems related to Find Mode in Binary Search Tree

Other problems that use the same Recursive Inorder Traversal and Tree Traversal Patterns (DFS & BFS) techniques:

Find Mode in Binary Search Tree FAQ

What is the LeetCode number of Find Mode in Binary Search Tree?

Find Mode in Binary Search Tree is LeetCode problem 501, rated Easy.

What pattern is used to solve Find Mode in Binary Search Tree?

Find Mode in Binary Search Tree is a Inorder Traversal problem. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Inorder Traversal subpattern.

What is the time complexity of Find Mode in Binary Search Tree?

The reference solution runs in O(n) time and O(h) space.

Which companies ask Find Mode in Binary Search Tree?

Find Mode in Binary Search Tree has been reported in interviews at Google.

Preparing this workspace.