题目:

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.

分析:
要得出二叉树中全部从根到叶子节点路径中,经过结点最少路径上的节点数。

採用二叉树的层序遍历思想。每遍历一层。高度加一,仅仅要遇到叶子节点,就终止,并返回当前层数。
代码:

class Solution {
public:
    int minDepth(TreeNode* root) {
        deque<TreeNode*> store;

        int left_num;
        int res = 0;
        if(!root)
            return res;
        store.push_back(root);
        while(!store.empty()) {
            res++;
            vector<int> res_t;
            left_num = store.size(); 
            while(left_num-- > 0) {
                const TreeNode* tmp = store.front();
                if(!tmp->left&&!tmp->right)
                    return res;
                store.pop_front();
                res_t.push_back(tmp->val);
                if(tmp->left)
                    store.push_back(tmp->left);
                if(tmp->right)
                    store.push_back(tmp->right);
            }
        }

    }
};