Monkey Patching Operators


  • 0

    Here I add operators <= and == to the TreeNode class. Mainly for fun and learning how to do Ruby's monkey patching.

    class TreeNode
      def ==(other)
        !self ? !other : !!other && self.val == other.val && self.left == other.left && self.right == other.right
      end
      def <=(other)
        self == other || !!other && (self <= other.left || self <= other.right)
      end
    end
     
    def is_subtree(s, t)
      t <= s
    end
    

Log in to reply
 

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