ruby in-order tree travers with monkey patch


  • 0
    G
    class NilClass
      def val
        0
      end
      def right
        nil
      end
      def left
        nil
      end
    end
    
    def merge_trees(t1, t2)
      return nil if t1.nil? && t2.nil?
      new_node = TreeNode.new(t1.val + t2.val)
      new_node.left = merge_trees(t1.left, t2.left)
      new_node.right = merge_trees(t1.right, t2.right)
      new_node
    end
    

Log in to reply
 

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