Minimum Depth of Binary Tree

update Jan 20,2018 10:24

LeetCodearrow-up-right

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.

Basic Idea:

这道题目的陷阱是要判断一个node是child需要判断他左右子树都为null,此时才能得到他的height。

两种思路: DFS 和 BFS。我认为 BFS 会比较好,因为BFS一定会先找到 minimum depth 之后就可以直接返回,而 DFS 需要搜索整个二叉树。

DFS Solution:

  • Java

    class Solution {
      public int minDepth(TreeNode root) {
          if (root == null) return 0;
          else if (root.left == null && root.right == null) {
              return 1;
          } else if (root.left == null) {
              return minDepth(root.right) + 1;
          } else if (root.right == null) {
              return minDepth(root.left) + 1;
          } else {
              return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
          }
      }
    }

BFS Solution:

  • Java

  • C++