Simple Python Solution with O(1) space In-order traversal


  • 0
    D
    class Solution(object):
    
        def findModeUtil(self,root):
            if not root:
               return 
            self.findModeUtil(root.left)
            if root.val == self.prev:
               self.cur += 1
            else:
               self.cur = 1
               self.prev = root.val
            if self.cur > self.modeCount:
               self.mode = [root.val]
               self.modeCount = self.cur
            elif self.cur == self.modeCount:
               self.mode += [root.val]
        
            self.findModeUtil(root.right)
            return 
        
    
        def findMode(self, root):
            if not root:
                return []
            self.prev = -100000000000 #use sys.maxint
            self.cur = 0
            self.modeCount = 0
            self.mode = []
            self.findModeUtil(root)
            return self.mode

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.