111. Minimum Depth of Binary Tree

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.

 

此题需要注意的是元素一侧没有子树的情况。比如下个case,没有右子树,所以是左边子树的最短路径+root的这个1。

 

public int MinDepth(TreeNode root) {
        if(root == null) return 0;
        if(root.left == null) return MinDepth(root.right)+1;
        if(root.right == null) return MinDepth(root.left)+1;
        return Math.Min(MinDepth(root.left), MinDepth(root.right))+1;
    }

 

posted @ 2016-09-11 22:28  咖啡中不塌缩的方糖  阅读(88)  评论(0编辑  收藏  举报