pic

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.

分析

  题目的意思是,给定一棵二叉树,求出它的最小深度。


     * 对树的问题,大多都可以使用递归来解决
     * 思路很清晰,编码也比较简单 

Code

class Solution {
public:
    /* 
     * 对树的问题,大多都可以使用递归来解决
     * 思路很清晰,编码也比较简单 
     * */
    int minDepth(TreeNode *root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        if(!root) return 0;
        int lChildDepth = 0, rChildDepth = 0;
        if(root->left)
            lChildDepth = minDepth(root->left);
        if(root->right)
            rChildDepth = minDepth(root->right);
        if(root->left && root->right)
            return 1 + std::min(lChildDepth, rChildDepth);
        if(root->left)
            return 1 + lChildDepth;
        if(root->right)
            return 1 + rChildDepth;
        return 1;
    }
};

pic