Wednesday, January 6, 2016

[Leetcode] Minimum Depth of Binary Tree

Problem

Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Analysis

  • Recursive calculate the depth of the tree
    • 1 + Math.min(left-tree, right-tree)
  • Need to deal with the case when left-tree or right-tree is null
    • If left-tree is null, return right-tree
    • If right-tree is null, return left-tree
  • Time: O(n)
    • As it traverses each node once

Code


No comments:

Post a Comment