Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Find Mode in Binary Search Tree

Posted on January 12, 2018July 26, 2020 by braindenny

Find Mode in Binary Search Tree



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).

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


## https://code.dennyzhang.com/find-mode-in-binary-search-tree
## Basic Ideas: In-order traversal
## Complexity:
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def findMode(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        res = []
        # in-order trasveral
        stack = []
        p = root
        while p:
            stack.append(p)
            p = p.left

        max_count = 0
        previous_val = None
        item_count = 0
        while len(stack) != 0:
            # print("item_count: %d, previous_val: %d, res: %s" % (item_count, previous_val if previous_val else -1, res))
            top_item = stack.pop()
            if previous_val is None:
                item_count = 1
                max_count = 1
                res.append(top_item.val)
            else:
                if top_item.val == previous_val:
                    item_count += 1
                else:
                    # reset the counter
                    item_count = 1
                if item_count == max_count:
                    res.append(top_item.val)
                else:
                    if item_count > max_count:
                        max_count = item_count
                        res = []
                        res.append(top_item.val)
            previous_val = top_item.val
            if top_item.right:
                p = top_item.right
                while p:
                    stack.append(p)
                    p = p.left
        return res
linkedin
github
slack

Post Views: 9
Posted in MediumTagged #binarytree, redo

Post navigation

LeetCode: Maximum Depth of Binary Tree
LeetCode: Symmetric Tree

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.