Wednesday, August 19, 2015

Leetcode 111. Minimum Depth of Binary Tree

https://leetcode.com/problems/minimum-depth-of-binary-tree/

Easy problem. Solve it elegantly.

Solution:
# T:O(n) S:O(h)
class Solution:
    # @param root, a tree node
    # @return an integer
    def minDepth(self, root):
        if root is None:
            return 0
        
        if root.left and root.right:
            return min(self.minDepth(root.left), self.minDepth(root.right)) + 1
        else:
            return max(self.minDepth(root.left), self.minDepth(root.right)) + 1
Run Time: 88 ms

No comments:

Post a Comment