Minimum Depth of Binary Tree

/**

 * Definition for a binary tree node.

 * struct TreeNode {

 *     int val;

 *     TreeNode *left;

 *     TreeNode *right;

 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}

 * };

 */

   

class Solution {

public:

    int minDepth(TreeNode* root) {

        if(root == NULL) return 0;

        dep(root,0);

    }

    

    int dep(TreeNode* root,int l){

        if(root==NULL) return INT_MAX;

        if(root -> left ==NULL && root->right ==NULL) return l+1;

        return min(dep(root->left,l+1),dep(root->right,l+1));

    }

};

注意题目要求,必须是到叶子结点。

你可能感兴趣的:(binary)